I have function handles, e.g. area_rectangle and area_circle, and structs that contain information like input_variables.height = 3.
function area_rectangle = area_rectangle(input_variables, constants) if ~isfield(input_variables,'height') || ~isfield(input_variables,'width') area_rectangle = NaN; else area_rectangle = input_variables.width * input_variables.height; endfunction area_circle = area_circle(input_variables, constants) if ~isfield(input_variables,'radius') || ~isfield(constants,'pi') area_circle = NaN; else area_circle = constants.pi * input_variables.radius^2; end
Unfortunately, cell arrays can't be used because they need some kind of index support:
>> test = cell(2,1) test = [] [] >> test{1}=@area_rectangle; >> test{2}=@area_circle; >> test test = @area_rectangle @area_circle >> input_variables = struct('height',3,'width',2,'radius',1) input_variables = height: 3 width: 2 radius: 1 >> constants = struct('pi',3.14159)constants = pi: 3.1416 >> test(input_variables,constants) Error using subsindex Function 'subsindex' is not defined for values of class 'struct'
How can I use function handles and structs at the same time?
Thanks in advance.
Best Answer