Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

1-line try/catch equivalent in MATLAB

I have a situation in MATLAB where I want to try to assign a struct field into a new variable, like this:

swimming = fish.carp;

BUT the field carp may or may not be defined. Is there a way to specify a default value in case carp is not a valid field? For example, in Perl I would write

my $swimming = $fish{carp} or my $swimming = 0; 

where 0 is the default value and or specifies the action to be performed if the assignment fails. Seems like something similar should exist in MATLAB, but I can't seem to find any documentation of it. For the sake of code readability I'd rather not use an if statement or a try/catch block, if I can help it.

like image 791
dannyhmg Avatar asked Apr 02 '15 19:04

dannyhmg


People also ask

What is try catch in Matlab?

You can use a try/catch statement to execute code after your program encounters an error. try/catch statements can be useful if you: Want to finish the program in another way that avoids errors. Need to clean up unwanted side effects of the error. Have many problematic input parameters or commands.

What is a try catch?

The try-catch statement consists of a try block followed by one or more catch clauses, which specify handlers for different exceptions. When an exception is thrown, the common language runtime (CLR) looks for the catch statement that handles this exception.


3 Answers

You can make your own function to handle this and keep the code rather clear. Something like:

swimming = get_struct(fish, 'carp', 0);

with

function v = get_struct(s, f, d)

if isfield(s, f)
    v = s.(f);   % Struct value
else
    v = d;       % Default value
end

Best,

like image 123
Ratbert Avatar answered Sep 22 '22 19:09

Ratbert


From what I know, you can't do it in one line in MATLAB. MATLAB logical constructs require explicit if/else statements and can't do it in one line... like in Perl or Python.

What you can do is check to see if the fish structure contains the carp field. If it isn't, then you can set the default value to be 0.

Use isfield to help you do that. Therefore:

if isfield(fish, 'carp')
    swimming = fish.carp;
else
    swimming = 0;
end

Also, as what Ratbert said, you can put it into one line with commas... but again, you still need that if/else construct:

if isfield(fish,'carp'), swimming = fish.carp; else, swimming = 0;

Another possible workaround is to declare a custom function yourself that takes in a structure and a field, and allow it to return the value at the field, or 0.

function [out] = get_field(S, field)
    if isfield(S, field)
        out = S.(field);
    else
        out = 0;
    end

Then, you can do this:

swimming = get_field(fish, 'carp');

swimming will either by 0, or fish.carp. This way, it doesn't sacrifice code readability, but you'll need to create a custom function to do what you want.

like image 35
rayryeng Avatar answered Sep 20 '22 19:09

rayryeng


If you don't like to define a custom function in a separate function file - which is certainly a good option - you can define two anonymous functions at the beginning of your script instead.

helper = {@(s,f) 0, @(s,f) s.(f)}
getfieldOrDefault = @(s,f) helper{ isfield(s,f) + 1 }(s,f)

With the definition

fish.carp = 42

and the function calls

a = getfieldOrDefault(fish,'carp')
b = getfieldOrDefault(fish,'codfish')

you get for the first one

a =  42

and the previous defined default value for the second case

b =  0
like image 40
Robert Seifert Avatar answered Sep 21 '22 19:09

Robert Seifert