Dear all,
I need to generate different indicators based on the same data but from a set of functions in a loop. Is there a way to specify different functions in a for loop?
Thanks.
Joe

댓글 수: 1

I seem to find a clumsy way myself involving eval:
for ii = 1:length(function_cell{ii})
hhh = eval(['@', function_cell{ii}]); % define a function handle for the ii-th function
zzz = hhh(inputs);
end
If there is any better approach, please let me know. Thanks.

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

 채택된 답변

Stephen23
Stephen23 2021년 5월 28일
편집: Stephen23 2021년 5월 28일

0 개 추천

Convert strings-of-function-names to function handles (which they should be anyway):
C = {'sin','sqrt','abs'};
C = cellfun(@str2func,C,'uni',0); % convert
for k = 1:numel(C)
C{k}(-1) % call
end
ans = -0.8415
ans = 0.0000 + 1.0000i
ans = 1

추가 답변 (1개)

Jan
Jan 2021년 5월 28일

0 개 추천

for ii = 1:length(function_cell)
zzz = feval(function_cell{ii}, inputs);
end

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2021a

태그

질문:

2021년 5월 28일

댓글:

2021년 5월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by