How to call a function name from function names saved in an array?

I have a combination of outputs. For example there is an output [1 0 0]. I have a set of functions saved the names of which I have saved in an array> i want to match the previous output numbers to call the corresponding function. One way I did was that I used sprintf to remove spaces and converted to
prev_output = [1 0 0]
fcs = sprintf('%d', prev_output);
funcs = ['func100';'func110'; 'func010'; 'func001'; 'func011'];
I want to call the function in the format below using some kind of code below
[output] = calledfunction(inputs)
I cannot use ismember function to call the function. Is there any way that this can be done? I will appreciate your help.

 採用された回答

Stephen23
Stephen23 2024 年 4 月 1 日
編集済み: Stephen23 2024 年 4 月 1 日
prev_output = [1,0,0];
fcs = sprintf('func%s',sprintf('%d', prev_output));
Either use FEVAL:
output = feval(fcs,inputs);
or STR2FUNC:
fnh = str2func(fcs);
output = fnh(inputs);

1 件のコメント

UH
UH 2024 年 4 月 1 日
I get it. Thank you! Works perfectly.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品

リリース

R2024a

質問済み:

UH
2024 年 4 月 1 日

コメント済み:

UH
2024 年 4 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by