Why function need more variables

1 次查看(过去 30 天)
(I'm a beginner of matlab. )
I try to make a matlab sub-function to analysis the frequency of our lab data,however,matlab always display "Not enough input arguments."
I can't fix error of my program.
What should I add or change?
function f = freq(time,data)
t = time;
d = data;
max = islocalmax(data);
dmax = d(max);
tmax = t(max); %the t of vmax
for i = 1 : length(dmax)-2
period(i) = tmax(i+2) - tmax(i);
end
f = 1/mean(period);
end

回答(1 个)

madhan ravi
madhan ravi 2019-8-8
编辑:madhan ravi 2019-8-8
time = ... some values
data = ... some values
f = freq(time,data) % this is how you call a function
Google "how to call a function MATLAB?"
Note: Don't use max as variable name because MATLAB has an inbuilt function named max()
  2 个评论
Zih-Sian Yang
Zih-Sian Yang 2019-8-8
编辑:Zih-Sian Yang 2019-8-8
I have used for loop to analysis the same data before,now,I want to creat a sub-function ,by chaning my for loop,to use. I want to know why program need more input argument.
In C,
double freq(double time,double data)
{
.
.
.
We can declare variable in there.
.
.
.
}
Why can't MATLAB declare variables in sub-functions?
I try to fixed by using global variables .
function f = freq(time,data)
global t d max dmax tmax
max = islocalmax(data);
dmax = d(max);
tmax = t(max); %the t of vmax
for i = 1 : length(dmax)-2
period(i) = tmax(i+2) - tmax(i);
end
f = 1/mean(period);
end
However,it still display "Not enough input arguments." in command window.
madhan ravi
madhan ravi 2019-8-8
编辑:madhan ravi 2019-8-8
https://in.mathworks.com/help/matlab/math/parameterizing-functions.html - the link shows you how to parameterize a function , plus you need to call the function properly with passing in the adequate amount of arguments to the function as shown in the answer above. Don‘t use global!, it’s completely avoidable.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by