0
votes

I'm working on a MATLAB app that programatically creates anonymous functions to evaluate any native MATLAB function and pass it a list of variables as argument. In the example below, 'formula' contains a string with the function and arguments to be evaluated (e.g., "sum( var1, var2 )" ). The formulas sometimes contain function calls nested within function calls, so the code below would be used recursively until obtaining the final result:

Func2 = str2func( sprintf( '@(%s) %s', strjoin( varNames, ',' ), formula ) );

This evaluates fine for native MATLAB functions. But there's a particular case of a function (named Func1) I made myself that not only needs the list of variables but also an object as argument, like this:

function output = Func1( anObject, varNames )
    % do some stuff with the object and the vars
end

For this particular function, I've tried doing this:

  Func2 = str2func( sprintf( '@(%s,%s) %s',  "objectToPassToFunc1", strjoin( varNames, ',' ), "Func1(objectToPass,""" + strjoin( varNames, '","' ) +""")" ) )

...which doesn't throw an error, but Func1 doesn't receive the objectToPassToFunc1, instead it gets values from one of the variables in varNames. And I don't know why.

So how can I correctly pass the object to Func1????

1
Your question is unclear, and what you are doing looks pretty inefficient. What are you really after? Do you know about feval? - Jonathan H
Looks like a bit more context is needed. The application looks at a list of formulas written by a user in a text file and then evaluates them one by one. Some of these formulas might contain nested function calls, so the app recursively evaluates them, by building anonymous functions. - alfavictor
I don’t understand the goal and I don’t understand the problem. Why would you need an anonymous function to evaluate a function? Why would passing an object be any different than passing any other variable? - Cris Luengo
@alfavictor So your users write valid Matlab formulas in a text file, which is therefore a valid Matlab script. Why not run it as such? - Jonathan H
@alfavictor I know you think that using anonymous functions in that case is the right thing to do, but if you are taking each line and putting it into an anonymous function, you are implicitly assuming the file is a valid Matlab script, regardless of whether they know Matlab or not. If you need variables to be defined for that script to run, that's easy to do. Otherwise I agree with Cris Luengo that there is no difference passing an object vs passing an argument to a function, so it's unclear what the problem is. - Jonathan H

1 Answers

1
votes

Matlab doesn't care about the type of arguments you pass to a function. As a matter of fact, the input could be scalar, vector, matrix, and even an object of a class. See the following example.

classdef ClassA
    methods
        function print(~)
            disp('method print() is called.');
        end
    end
end

This class has only one method. Now, let us define an anonymous function func which accepts one input.

func = @(arg) arg.print;

Notice that we explicitly assume that the input is an object of ClassA. If you pass another type of data to this function, Matlab will throw an error. To test the code,

obj = ClassA;
func = @(arg) arg.print;
func(obj)

To avoid the error, you may need to check the type of the input before using it. For example,

function [] = func(arg)

% check if arg is an object of ClassA
if isa(arg,'ClassA')
    arg.print;
end

end

Now you can pass different types for the input without getting an error.