I have a Matlab function name as a string (the variable name is 'function_name'), which is an input from a user, and I need to somehow call that function from Python. Below you can see how I was trying to call this function using the variable name, but it exited with the following error: (<class 'matlab.engine.MatlabExecutionError'>, MatlabExecutionError("Undefined function 'chsn_agthm' for input arguments of type 'uint8'.\n"), <traceback object at 0x0000015D14323C00>)
import matlab.engine
eng = matlab.engine.start_matlab()
res = eng.function_name(input1, input2, nargout=2)
The problem is that
function_nameneeds to be a statement in the python code, but is given as string. To resolve this, my suggestion would be not to callfunction_namedirectly, but to always call MATLAB'sfevalfunction withfunction_nameas first input argument, i.e.fevalis then called by the MATLAB engine and invokes the function with the namefunction_nameusing the other arguments (apart fromnargout=2) as input.For further information on
fevalplease have a look at https://de.mathworks.com/help/matlab/ref/feval.html.Alternative approach:
It might also be possible to construct a string that contains a valid python expression including
function_nameand then to call this expression using pythonseval().