Function names and parameters in Matlab variables

In my matlab ml file I am using some logic (string concat) to create variables like this:

c = 'CalcPrediction(1,10)'

      

This means that I have a string which is a function and some parameters. How can I make this function call?

Attempting to run (c) results in:

>> run(c)
??? Error using ==> run at 71
CalcPrediction(1,10) not found.

      

Note: run (c) works fine if there are no parameters. For example. c = 'CalcPrediction'; execution (s);

0


source to share


3 answers


The command you are looking for is eval()

insteadrun()



+7


source


Without actually looking at the script, it's hard to generalize, but ...

Where squareRoot is m file containing only: y=sqrt(x)

Then it runs:

x=[2,0];

c='squareRoot';

run(c);



gives:

y =

1.4142 0

This example means that you can define a script to use the declared variable ( x

in this case) and then declare the variable before running the script.

Without the script, I don't know what you are doing with the parameters. If that doesn't answer your question, please post to the script.

0


source


You want to use str2func . This function takes a string and returns a function handler that can be called with your parameters. Check out the examples on the linked page.

fh = str2func('CalcPrediction')
fh(1, 10)

      

-1


source







All Articles