Referring to specific input in loop

조회 수: 1 (최근 30일)
Ahmed Abdulla
Ahmed Abdulla 2020년 6월 8일
댓글: Stephen23 2020년 6월 8일
I am writing a function and the input arguments are Data1 Data2 Data3 Data4 .... DataN. I am running a loop from i=1:N and one of the commands in this loop is to get the size(Datai) but im not sure how to refer to the specifc Data matrix based on the value of i in the loop.
  댓글 수: 2
KSSV
KSSV 2020년 6월 8일
편집: KSSV 2020년 6월 8일
How/ Why there is Data1, Data2, ...DataN?
This is not allowed/ comfortable. Rather use Data{1}, Data{2},.....Data{N}. This is called cell.
Stephen23
Stephen23 2020년 6월 8일
"...the input arguments are Data1 Data2 Data3 Data4 .... DataN."
Numbering variable names like that is a sign that you are doing something wrong.
Most likely your code would be simpler and more efficient if you just used one container array, e.g. a cell array.

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

채택된 답변

Mohammad Sami
Mohammad Sami 2020년 6월 8일
If you want to have variable number of arguments, perhaps you want to use varargin instead in your function signature.
function out = f_take_n_args(varargin)
for i = 1:length(varargin)
Datai = varargin{i};
end
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Objects에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by