Unable to accept a vector as parameter in user defined function.
Mostrar comentarios más antiguos
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
Respuestas (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 comentarios
Vignesh Ramakrishnan
el 3 de Oct. de 2020
Editada: Vignesh Ramakrishnan
el 3 de Oct. de 2020
"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
el 3 de Oct. de 2020
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!