unable to overcome syn_sin error message

3 views (last 30 days)
Jacob
Jacob on 12 Sep 2023
Answered: Walter Roberson on 12 Sep 2023
I am trying to write following script file in matlab for an assignment
function [xx,tt]=syn_sin(fk, Xk,fs,dur,tstart)
if nargin<5
error('input arguments for the function syn_sin required more')
elseif nargin ==5
tstart = 0;
end
k=length(fk);
if k~=length(Xk)
error('Here fk,Xk should be equal elements');
end
[xx,tt]=one_cos(1,0,0,fs,dur,tstart);
for i=1:1:k
xx=xx+one_cos(fk(i),abs(Xk(i)),angle(Xk(i)),fs,dur,tstart);
end
However I am not able to get this file to do anything but error and I get the following error below
Error using syn_sin
input arguments for the function syn_sin required more
Nothing I try works and I have to use syn_sin so I don't know why there is an error.

Answers (1)

Walter Roberson
Walter Roberson on 12 Sep 2023
You probably want:
  • if nargin<4 give an error message
  • if nargin == 4 set tstart = 0
Your current code does not allow 6 or more inputs because there are only 5 input positions and the final one is not varargin
Your current code does not allow 0, 1, 2, 3, or 4 inputs because that is < 5 .
So when you reach the elseif you know that nargin must be 5 exactly. But then you test for 5 (which is certain to be the case if you reach there) and if it is true (which it always will be if you reach the elseif in this code) then you set tstart = 0 -- overwriting any tstart the user might have passed in.

Categories

Find more on Software Development Tools in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!