plotFunction = input('Specify a plotting function: ','s');
data = input('Enter data to plot: ');
feval(plotFunction,data)
Field Names in Variables
Access data in a structure with a variable field name by enclosing the expression for the field in
parentheses. For example:
myData.height = [67, 72, 58];
myData.weight = [140, 205, 90];
fieldName = input('Select data (height or weight): ','s');
dataToUse = myData.(fieldName);
If you enter weight at the input prompt, then you can find the minimum weight value with the
following command.
min(dataToUse)
ans =
90
For an additional example, see “Generate Field Names from Variables” on page 11-10.
Error Handling
The preferred method for error handling in MATLAB is to use a try, catch statement. For example:
try
B = A;
catch exception
disp('A is undefined')
end
If your workspace does not contain variable A, then this code returns:
A is undefined
Previous versions of the documentation for the eval function include the syntax
eval(expression,catch_expr). If evaluating the expression input returns an error, then eval
evaluates catch_expr. However, an explicit try/catch is significantly clearer than an implicit
catch in an eval statement. Using the implicit catch is not recommended.
2 Program Components
2-88
Précédent

- 138/1450

Suivant