MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
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-13.

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

Free download pdf