Errors occurred during parsing of MATLAB function 'MATLAB Function'

16 views (last 30 days)
Hello,
I am trying to create a simulink model for a function in MATLAB and I keep getting the following error (Errors occurred during parsing of MATLAB function 'MATLAB Function'). I tried to declare the vectors to be zero before I start the for loop but still it did not work. The code for that part of the function is the following:
function p = shep(number_tones,f1,f2,tmax,sr)
% % PARAMETERS % %
%number_tones=3; % number of tones
%f1 = 25; % starting freq (Hz)
%f2 = (2^number_tones)*f1; % ending freq (Hz)
%f2 = 130.81; % %tmax=18; % duration (sec)
%sr=48100; % sample rate (samples/sec)
dt=1./sr; % time step
ns=tmax*sr; % number of sample/cycles
cycles=1; % number of cycles
% oct=log(f2/f1)/log(2.); % number of octaves
ntimes = ns*cycles; % t2=tmax; dur=tmax; % tpi=2.*pi; % rate=oct/dur; %
TT=linspace(dt,(ntimes+1)*dt,ntimes); %
a = zeros(1,ntimes);
arg = zeros(1,ntimes);
freq = zeros(1,ntimes);
a=zeros(1,ntimes);
theta=zeros(number_tones,1);
arg=zeros(number_tones,1);
key=zeros(number_tones,1);
noc=zeros(number_tones,1);
amp=zeros(number_tones,1);
% for i=1:number_tones
noc(i)=2^(i-1);
end %
fspan=f2-f1;
theta_coefficient=tpi/(rate*log(2));
%
sd=150;
for i=1:ntimes
% W=-1.+2^(rate*TT(i));
%
arg(1)=f1*W;
%
for j=1:number_tones
if(key(j)==0)
arg(j)=arg(1)*noc(j);
else
W=-1.+2^(rate*TT(i-key(j)));
arg(j)=f1*W;
end
end
%
for j=1:number_tones
fspectral=noc(j)*f1*(2^(rate*TT(i-key(j))));
delta=(fspectral-f1)/fspan;
amp(j)=(1-cos(tpi*delta));
%
if(fspectral>f2)
key(j)=i;
noc(j)=1;
end
end
%
theta=theta_coefficient*arg;
%
a(i)=sum(amp.*sin(theta));
%
end
%
a(1)=0.;
Thank you,
Tasos
  1 Comment
Kaustubha Govind
Kaustubha Govind on 19 Jun 2014
Please provide the additional error messages (they should point to the specific code that caused the failure).

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 19 Jun 2014
Edited: Geoff Hayes on 19 Jun 2014
Tasos - please edit your above code so that is formatted properly. Just highlight the code portions and press the {} Code button. Or, since there are several dozen lines, just add the code as an attachment to your question.
One problem with the above, which prevents the code from being run, is the way you are using comments, %. Unlike C++, there is no start comment /* and end comment */ which is what I think you were attempting at
% for i=1:number_tones
noc(i)=2^(i-1);
end %
All the above does is comment out the for statement but leaves the noc assignment and end alone - so one line has been commented out only and not this block of code. You must do something like
% for i=1:number_tones
% noc(i)=2^(i-1);
% end
to comment out the block. The MATLAB editor makes this easy by allowing you to highlight a block of code and press the % button in the menu to comment out that block.
Further code fails because you have commented out tpi but still make use of it in future lines. And the same is true for other variables too ( oct, dur, rate).
Finally, the variable fspan is used but never defined.
Once these problems have been resolved, then hopefully the code will either work correctly or the MATLAB error message will be more clear as to what is causing a problem.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!