Call function, when the function name is saved in variable

10 ビュー (過去 30 日間)
Volker Schäfer
Volker Schäfer 2017 年 10 月 11 日
コメント済み: Walter Roberson 2017 年 10 月 11 日
Hi,
i would like to call a function, but i do not know the name of the function when coding. The name of the function is saved in a variable. Please see the code example. I create several Neuronal Nets and would like to open them to compare the performance:
for number=1:1000;
fcn_name = ['Neuronal_Net_' int2str(number)] % Create Name
edit (fcn_Name); % Open the m.file with the function
Results(number) = fcn_Name(Data) % This line is NOT working. Here i need help.?????????????????
end
I could write, the following very long code.
Result(1) = Neuronal_Net_1(Data)
Result(2) = Neuronal_Net_2(Data)
...
Thanks in Advance for your support.

採用された回答

Walter Roberson
Walter Roberson 2017 年 10 月 11 日
clear( fcn_name ) %brackets are important here
Results(number) = feval(fcn_name, Data);
  2 件のコメント
Volker Schäfer
Volker Schäfer 2017 年 10 月 11 日
Works perfektly, thanks!!!
Why do i need to do "clear( fcn_name)"?
Walter Roberson
Walter Roberson 2017 年 10 月 11 日
You appear to have edited your code. Your original code called a routine that generated the function. Whenever you dynamically generate a function into a file, if there is any possibility that you might have already called upon the old version, you should clear giving the name of the function, to force MATLAB to flush any already-parsed version of the file it might have cached in memory. (MATLAB does that for all or most files it executes, to make it more efficient in case the routine is called again.)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLow-Level File I/O についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by