필터 지우기
필터 지우기

Passing Variable Number of Arrays to a function with varargin

조회 수: 2 (최근 30일)
Hello,
I would like to have a function that will take either one array or a number of them merged into one array within the function, which will then be plotted. The beginning of it is as follows:
function temp_array = fft100v(first_array,varargin)
% Plots the first 100 frequencies in in_array
% This assumes that sample interval is 1000 samples per second
% If length of array is shorter or longer than 1000, pad or cut it as
% appropriate
len_orig = length(first_array);
if nargin > 1
for n = 2:nargin
temp = varargin{n};
if length(temp) ~= len_orig
error('Arrays not of equal length');
end
end
end
The program craps out at the line "temp = varargin{n}" for more than one input with "Index exceeds matrix dimensions". It DOES have the curly braces on the "n". Guess I don't know enough about cell arrays.
Thanks.
Doug Anderson

채택된 답변

random09983492
random09983492 2013년 6월 20일
Hi Doug,
varargin starts at index 1, not index 2. That should fix it up.
  댓글 수: 1
Douglas Anderson
Douglas Anderson 2013년 6월 20일
Thanks!
I had to figure out how nargin relates also.
if nargin > 1
for n = 1:nargin-1
temp = varargin{n};
if length(temp) ~= len_orig
error('Arrays not of equal length');
end
end
end
The loop can't go to the total nargin value, since the first one is also counted in the nargin!
Much appreciated.
Doug

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Digital Filter Analysis에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by