the function can only take one input argument

조회 수: 10 (최근 30일)
Nawaf Aldhawi
Nawaf Aldhawi 2022년 6월 8일
편집: Jan 2022년 6월 8일
so when i a make a function called analyzeT to analyze temperatures i can't enter the temp's this way "analyzeT(28,44,50,33)' it tells me that there's too many input arguments but when i make a vector first and store the values in it then i write 'analyzeT(vector)' it works, anyway i can make the first way work? thanks!
this is the function
function [maxT,minT,avgT] = analyzeT(temp)
i = length(temp);
maxT = temp(2);
minT = temp(1);
sum = 0;
for i = 1:i
if temp(i) > maxT
maxT = temp(i);
elseif temp(i) < temp(1)
minT = temp(i);
else
end
sum = temp(i) + sum;
end
avgT = sum/length(temp);
end
  댓글 수: 1
Jan
Jan 2022년 6월 8일
편집: Jan 2022년 6월 8일
Why do you assume, that maxT cannot be temp(1) ?
This is not a but, but very ugly:
i = length(temp);
for i = 1:i
If maxT and minT are set to temp(1), you do not have to check temp(1) again.
Together:
nTemp = length(temp);
maxT = temp(1);
minT = temp(1);
sumTemp = temp(1);
for i = 2:nTemp
...
end
Avoid using the names of builtin functions as variables. This is not an error, but a frequent source of unexpected behavior if you use the function sum() later.
I do not think, that this is exactly what you want:
elseif temp(i) < temp(1)
minT = temp(i);
else % Omit this orphaned else, if it is not used.
end
Do you really want to compare the temperatures with temp(1), or with minT?

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

채택된 답변

Jan
Jan 2022년 6월 8일
편집: Jan 2022년 6월 8일
Either modify the call to create the vector dynamically:
analyzeT([28,44,50,33])
% ^ ^
Or create a new input interface for the function (which is not efficient or nice in this example):
function [maxT,minT,avgT] = analyzeT(varargin)
if nargin == 1
temp = varargin{1};
else
remp = [varargin{:}]; % Concatenate inputs to a vector
end
...

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by