Alternative to using EVAL

조회 수: 6 (최근 30일)
Andrew
Andrew 2013년 3월 3일
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

채택된 답변

Cedric
Cedric 2013년 3월 4일
편집: Cedric 2013년 3월 4일
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
Cedric
Cedric 2013년 3월 4일
편집: Cedric 2013년 3월 4일
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 2013년 3월 5일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 3월 3일
If your goal is to use different functions
f=@myModel
[jfit, estParams, graphData] = f(tr, Pr, Qr, Avr, powerfactor, WarmStartIdx, graphDataStruct);
  댓글 수: 1
Azzi Abdelmalek
Azzi Abdelmalek 2013년 3월 4일
편집: Azzi Abdelmalek 2013년 3월 4일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by