How to call a function by assigning a string to a variable?

13 vues (au cours des 30 derniers jours)
Arif Ahmed
Arif Ahmed le 21 Déc 2020
Modifié(e) : Arif Ahmed le 21 Déc 2020
Please consider the following:
I write a function file named 'case_a' from a function 'test'.
output = 'case_a';
matlabFunction(test, 'file', output, 'Optimize', false);
Because my variable output is going to be changing every iteration, I would like to be able to call the actual output function. Specifically, let's say I run a new simulation and I would like to call the function case_a(input) with some input vector. But I would like to first assign the saved function file to my 'output' variable such that I can call output(input) in every run. In this case output should actually be the case_a. In another case I want to assign 'case_b' in output, such that I can still call it as output(input).
Regards
  3 commentaires
Stephen23
Stephen23 le 21 Déc 2020
The description is not very clear, but it does have quite a smell to it:
"... should actually be the case_a. In another case I want to assign 'case_b' in output..."
Arif Ahmed
Arif Ahmed le 21 Déc 2020
Let me try again.
I run a simulation, which gives me an output file named 'case_a.m'
output = 'case_a';
matlabFunction(test, 'file', output, 'Optimize', false);
I run another simulation, which gives me an output file named 'case_b.m'
output = 'case_b';
matlabFunction(test, 'file', output, 'Optimize', false);
I, later, want to repeat the calculation with some new data for both 'case_a' and 'case_b' functions. However, I want to automate calling them by running a loop as I have the function names in a string such as
function_name = ['case_a'; 'case_b']
How can I execute the functions from the list in function_name without having to write case_a and case_b manually.
Does this make any sense?

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 21 Déc 2020
Modifié(e) : Matt J le 21 Déc 2020
feval(output,arg1,arg2,...)
or
fun=str2func(output);
fun(arg1,arg2,...)
  1 commentaire
Arif Ahmed
Arif Ahmed le 21 Déc 2020
Thank you very much!
This is what I needed.
fun=str2func(output);

Connectez-vous pour commenter.

Plus de réponses (1)

Matt J
Matt J le 21 Déc 2020
Modifié(e) : Matt J le 21 Déc 2020
Since you've set Optimize=false, I don't know why you are sending the result to a file. You may as well just use the output of matlabFunction() directly,
fun=matlabFunction(test);
fun(arg1,arg2,...)
  3 commentaires
Matt J
Matt J le 21 Déc 2020
But once fun is created, you can reuse it and/or save it to a file. Why would you need to call matlabFunction every time?
Aside from that, if matlabFunction is taking a long time, it likely means you are doing something symbolically that should really be done numerically.
Arif Ahmed
Arif Ahmed le 21 Déc 2020
Modifié(e) : Arif Ahmed le 21 Déc 2020
I am calling matlabFunction for all the different problems that I need to investigate. Essentially making function files that have 6,000 to 12,000 equations in some of them.
Once they are saved, I can reuse them later even if I clear the workspace. I just needed to know how to call them, which str2func does.
I am using the symbolic expressions as I need to get the Jacobian out of those 12,000 equations.
I am just trying out different coding/solution approaches. Trying to investigate what are the differences in coding and execution.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by