필터 지우기
필터 지우기

Use a for loop to generate function arguments

조회 수: 8 (최근 30일)
Georgios Pyrgiotakis
Georgios Pyrgiotakis 2022년 12월 8일
댓글: Voss 2022년 12월 9일
I want to use a function myfunction in a loop that can take an increasing number of arguments. I also want to save the result into a variable that each time has a different name. The result is also a vector. For example
for i=1:50
data_i=myfunction(d,d,d,...) %The number of arguments will nicrease
end
In other words I want to create this:
i=1 data_1=myfunction(d)
i=2 data_2=myfunction(d, d)
i=3 data_3=myfunction(d, d, d)
i=4 data_4=myfunction(d, d, d, d)
i=5 data_5=myfunction(d, d, d, d, d)
I found some similar questions but none of the answers were suitable.
Thanks for your help.

채택된 답변

Voss
Voss 2022년 12월 8일
n = 50;
data = cell(1,n); % cell array to contain the output from my_function for all iterations
input_args = {}; % cell array to contain input arguments for my_function
for ii = 1:n
input_args{ii} = d; % add a new input argument
data{ii} = my_function(input_args{:}); % call my_function with all the input arguments defined so far,
end % and put the result in cell ii of data.
  댓글 수: 2
Georgios Pyrgiotakis
Georgios Pyrgiotakis 2022년 12월 9일
Thank you that's what I needed.
Voss
Voss 2022년 12월 9일
You're welcome!

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

추가 답변 (1개)

VBBV
VBBV 2022년 12월 8일
data_i(i)=myfunction(d,d,d,...)
  댓글 수: 3
Georgios Pyrgiotakis
Georgios Pyrgiotakis 2022년 12월 8일
the funciton output is a vector and I want to keep it separate.
data_1=[ ...]
data_2=[ ...]
data_3=[ ...]
VBBV
VBBV 2022년 12월 8일
data_i{i}=myfunction(d,d,d,...)
In that case use a cell array

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by