executing an m-file using a string for calling
19 ビュー (過去 30 日間)
古いコメントを表示
Hello, I am trying to find a way to execute an m-file from a string.
For example I get from a DB return the name myfunc(1,2) as a char and I want to execute the function myfunc.m with the (1,2) as variables. Does anyone have any solution.
Thanx in advance.
Georgios Mavropoulos
0 件のコメント
採用された回答
Walter Roberson
2011 年 3 月 3 日
%somehow before this, DBString = 'myfunc(1,2)'
eval(DBString)
7 件のコメント
Mathijs Franssen
2021 年 2 月 18 日
Excellent, thanks a million Stephen! Wasn't aware of str2func, first time I've ran into this sort of issue. This helps!
Jürgen
2021 年 11 月 30 日
for me 'Stephens comment' of 28th,jan. 2021 should be worked out more compact and moved to the top-rated answer to the question.
I was also searching for something like (list of) function reference defined during runtime.
The str2fun seems the way to go with respect to all the points in Stephens comment (code analysis, syntax checking, tracing variables, code hints, tab completion, input hints, variable highlighting, debugging, security).
その他の回答 (4 件)
Jan
2011 年 3 月 17 日
If you call RUN with a string, which contains the path, the file extension .m is removed. Without the path, .m is kept.
mfile = dir('layer*.m');
[trash, name] = fileparts(mfile(1).name);
run(name);
Or:
mfile = dir('layer*.m');
run(fullfile(cd, mfile(1).name));
Note: "mfile(1)" considers, that DIR can find multiple matching files.
Dimitris
2011 年 3 月 15 日
I'm trying to do something similar. My output is an .m file. Like this:
mfile = dir('layer*.m')
So, afterwards I want to run the file named layer<something>.m But if I do as you say:
eval(mfile.name)
I get this error:
??? Undefined variable "layer2" or class "layer2.m".
Why is this happening? The file I'm trying to run, layer2.m, is in the current directory.
3 件のコメント
Jan
2011 年 3 月 17 日
RUN removes the file extension only, if the name contains the path:
working: run('C:\MFiles\layer2.m')
failing: run('layer2.m')
I think, this is a bug in RUN: If "script" does not contain a path, "exist(script, 'file')" checks the existence before "evalin('caller', [script, ';'])". But if "script" is an existing file, the file extension let EVALIN fail. "evalin('caller', [s, ';'])" would work. (checked in 2009a)
Dimitris
2011 年 3 月 15 日
I tried to use:
run(mfile.name)
but I got the same error:
??? Undefined variable "layer2" or class "layer2.m".
Error in ==> run at 74
evalin('caller',[script ';']);
:S I also tried
run(eval(mfile.name))
with the same results... Thank you for your interest!
2 件のコメント
Jan
2017 年 11 月 12 日
@Hayatullahi Adeyemo: The variable "mfile.name" contains the string 'layer2'. The error message produced by run('layer2') means, that there is no function with this name in Matlab's path. Then eval('layer2') will not work also.
参考
カテゴリ
Help Center および File Exchange で Debugging and Analysis についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!