Alternative to using EVAL

6 vues (au cours des 30 derniers jours)
Andrew
Andrew le 3 Mar 2013
Hi All,
I need to call a function whose name is decided dynamically at runtime. Is there an alternative to using the eval function?
My requirement is this: I have a Matlab application with a non-linear optimisation model as the core solver for this application. I need the user to have the option to "plug and play" different models without needing to rewrite the code.
Currently I have solved this requirement by having a string array containing the name of the model function (e.g. LoadModelPkg = 'myModel'). The user can change this using a dialog box.
My application then calls this function using the following lines of code:
LoadModelPkg = 'myModel' % name of the function to call.
LoadModelPkgCall = ['[jfit, estParams, graphData] = ', LoadModelPkg, '(tr, Pr, Qr, Avr, powerfactor, WarmStartIdx, graphDataStruct);']; % create the function call
eval(LoadModelPkgCall)
This works, but the Matlab debugger warns that the variables in the above LoadModelPkgCall string are unused. Is there an alternative to using the eval function?
Thanks for the help.
Andrew

Réponse acceptée

Cedric
Cedric le 4 Mar 2013
Modifié(e) : Cedric le 4 Mar 2013
If the name of your function must really be a string, you'll want to use STR2FUNC to build a function handle out of the string, and then use it as in Azzi's answer.
fn = 'MyModel' ; % Function name, coming from elsewhere.. e.g. some user
% input management mechanism.
fh = str2func(fn) ; % Function handle.
[jfit, estParams, graphData] = fh(tr, Pr, etc..) ;
  4 commentaires
Cedric
Cedric le 4 Mar 2013
Modifié(e) : Cedric le 4 Mar 2013
I meant that if you give the user to possibility to choose between pred-defined models/functions that you (and only you) define, then handles to these functions are the way to go (no need to use STR2FUNC). Also when you want to give users the possibility to "pass a function" to your function, handles are the way to go (i.e. I, as a user, prefer to pass a handle to a function that I chose than the name of this function as a string). Now if there is a GUI involved and users can define some path + function name, it is better/simpler to accept a function name as a string and convert it into a handle using STR2FUNC, than to use some complicated construct based on EVAL. So I think that there is no better alternative than what you ended up implementing!
Andrew
Andrew le 5 Mar 2013
Thanks for that Walter and Cedric. Then I'll stick with str2func since I'm receiving a path/name from the user. I replaced eval with str2func and it works well, and it simplified the code a bit. It also became more readable.
Thanks for your help guys.

Connectez-vous pour commenter.

Plus de réponses (1)

Azzi Abdelmalek
Azzi Abdelmalek le 3 Mar 2013
If your goal is to use different functions
f=@myModel
[jfit, estParams, graphData] = f(tr, Pr, Qr, Avr, powerfactor, WarmStartIdx, graphDataStruct);
  1 commentaire
Azzi Abdelmalek
Azzi Abdelmalek le 4 Mar 2013
Modifié(e) : Azzi Abdelmalek le 4 Mar 2013
You can create a function where you will past a handle function, You will need also to use str2func as suggested by Cedric.
Example
function y=fcn(f,data,choice)
% y and data are cell array
% f is you function
if choice==1
[a,b]=f(data{1},data{2})
y={a,b}
elseif choice==2
y=f(data{1}
else
[a,b,c}=f(data{1},data{2},data{3})
y={a,b,c}
end

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programming 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