필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

How can I get the input arguments of a function and save them into an array ?

조회 수: 1 (최근 30일)
Bo Sarah
Bo Sarah 2018년 11월 9일
마감: MATLAB Answer Bot 2021년 8월 20일
Hi everyone,
I'd like to know how to get the input arguments of a function and put them into an array to be able to do a loop. I'm also looking of len(["file1.csv","file2.csv","file3.csv"]) =3 in matlab. Currently, I'm getting this : length(['file1.csv','file2.csv','file3.csv']) =27 and as I want to pass this array to a function to read the 3 files one after an other with a loop. Could you help me please ?
I thank you in advance for all your answers.
  댓글 수: 2
Walter Roberson
Walter Roberson 2018년 11월 12일
Be careful about single quotes versus double quotes. In r2017a and later you can write double quotes in order to indicate string objects. The syntax you used first with double quotes is fine and would give you length 3. The second syntax with single quotes you are constructing a single character vector and you cannot reliably pull it apart again without making assumptions such as that the only extension that will ever occur is csv.
Stephen23
Stephen23 2018년 11월 12일
편집: Stephen23 2018년 11월 12일
@Bo Sarah: these are NOT the same:
["file1.csv","file2.csv","file3.csv"] % 1x3 string array
['file1.csv','file2.csv','file3.csv'] % 1x27 character vector
Do NOT confuse character arrays with string arrays. They have very different properties:

답변 (1개)

Ashutosh Prasad
Ashutosh Prasad 2018년 11월 12일
You can define your function with a variable length argument list using varargin and then use the index of the cell array to refer to each of your input arguments. Given below is a sample function definition.
function var_arg(varargin)
if nargin == 3
disp(varargin(1));
disp(varargin(2));
disp(varargin(3));
end
end
length(["file1.csv","file2.csv","file3.csv"]) gives an output 3.
  댓글 수: 1
Stephen23
Stephen23 2018년 11월 12일
"length(["file1.csv","file2.csv","file3.csv"]) gives an output 3."
Sure, but that string array is not the same as the character vector in the original question.

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by