Unable to accept a vector as parameter in user defined function.
显示 更早的评论
I made a function to add a set of 'n' sine terms as per my algorithm.
function inst_amplitude = sincustom(t,[FreqList],[AmpList])
%UNTITLED3 Summary of this function goes here
% Detailed explanation goes here
l_freq=length(FreqList);
l_amp=length(AmpList);
result=0;
if l_freq ~= l_amp
result=0;
else
for i=1:l_amp
result=result+(AmpList[i].*sin(t.*FreqList[i]));
end
end
return result;
end
回答(1 个)
A valid function definition requires the name of the input argument/s (no square brackets like you used):
function result = sincustom(t,FreqList,AmpList)
Also note that indexing in MATLAB uses parentheses (not square brackets like you used):
result = result + AmpList(i).*sin(t.*FreqList(i));
3 个评论
Vignesh Ramakrishnan
2020-10-3
编辑:Vignesh Ramakrishnan
2020-10-3
"Got this error"
Error: File: sawtoothcustom.m Line: 14 Column: 8
The code you posted has only 13 lines in total, and is named sincustom, so the error appears to occur in some code that you have not shown us. I cannot degug code that I cannot see, nor have any idea how it is being called.
Note that this line:
return result;
does not "return" the variable result, it simply returns exits the function at that point (note that the return documentation does not mention anythingn about it accepting an argument, so it is not clear what you expect if you provide it with one). If you want to return the variable named result, then you need to define it as an output argument (currently the output argument inst_amplitude is not defined anywhere), you do not need to use return at all:
function result = sincustom(t,FreqList,AmpList)
% ^^^^^^^ outputs are defined here!
Vignesh Ramakrishnan
2020-10-3
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!