Hi,
I'm trying to call a set of functions from the filesnames associated with them. The .m files will be dynamic and I won't be able to hard code them. I'm using a dir function to return a struct of the files in a specified directory and I'm trying call each function from the main script.
m=dir([myFolder '\models\*.m']);
models={m.name}
This gives models a 1 x 2 cell array
'modelexample1.m' 'modelexample2.m'
I need to be able to get the function name in each file. How can I do this? I think the functions can be called using feval, but if there are any tips onj this, please let me know.
Kind Regards, David

2 件のコメント

Kian Azami
Kian Azami 2017 年 9 月 27 日
Maybe if you type at the end of your code:
Updated_Model = models{end}
it will give you the updated name.
Stephen23
Stephen23 2017 年 9 月 27 日
"I need to be able to get the function name in each file"
Note that the documentation clearly states that " the best practice is to use the same name for the function and the file (in this example, fact.m), since MATLAB® associates the program with the file name"

サインインしてコメントする。

 採用された回答

Stephen23
Stephen23 2017 年 9 月 27 日
編集済み: Stephen23 2017 年 9 月 27 日

0 投票

S = dir(fullfile(myFolder,'models','*.m'));
for k = 1:numel(S)
[~,N] = fileparts(S(k).name);
fun = str2func(N);
fun(...)
% OR
%feval(S(k).name,...)
end

その他の回答 (1 件)

David Caldwell
David Caldwell 2017 年 9 月 27 日

0 投票

Brilliant! Thank You

カテゴリ

ヘルプ センター および File Exchange で File Operations についてさらに検索

製品

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by