Alternative to using EVAL
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Akzeptierte Antwort
Cedric
am 4 Mär. 2013
Bearbeitet: Cedric
am 4 Mär. 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 Kommentare
Cedric
am 4 Mär. 2013
Bearbeitet: Cedric
am 4 Mär. 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!
Weitere Antworten (1)
Azzi Abdelmalek
am 3 Mär. 2013
If your goal is to use different functions
f=@myModel
[jfit, estParams, graphData] = f(tr, Pr, Qr, Avr, powerfactor, WarmStartIdx, graphDataStruct);
1 Kommentar
Azzi Abdelmalek
am 4 Mär. 2013
Bearbeitet: Azzi Abdelmalek
am 4 Mär. 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
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!