From what I know, you cannot do this on one line in MATLAB. MATLAB logical constructs require explicit operators if/elseand cannot do this on the same line ... as in Perl or Python.
What you can do is check if the structure contains a fishfield carp. If it is not, you can set the default value to 0.
Use isfieldto help you with this. Therefore:
if isfield(fish, 'carp')
swimming = fish.carp;
else
swimming = 0;
end
Also, as Ratbert said, you can put it on one line with commas ... but again, you still need a construct if/else:
if isfield(fish,'carp'), swimming = fish.carp; else, swimming = 0;
, , 0.
function [out] = get_field(S, field)
if isfield(S, field)
out = S.(field);
else
out = 0;
end
:
swimming = get_field(fish, 'carp');
swimming 0, fish.carp. , , , , .