Why my code is not working, showing the error "Illegal use of reserved keyword "elseif"

23 次查看(过去 30 天)

Parse error: Illegal use of reserved keyword "elseif".

Function 'MATLAB Function' (#23.614.615), line 44, column 5: "e" Launch diagnostic report.

function [SaT, SaB, SbT, SbB, ScT, ScB] = pwm(u) f = 50; ts=0.0002; vdc=1; peak_phase_max = vdc/sqrt(3);

x=u(2);

y=u(3); mag=(u(1)/peak_phase_max) * ts;

% sector I if (x>=0) && (x<pi/3)

ta = mag * sin(pi/3-x); tb = mag * sin(x); t0 =(ts-ta-tb); t1=[t0/4 ta/2 tb/2 t0/2 tb/2 ta/2 t0/4]; t1=cumsum(t1);

v1=[0 1 1 1 1 1 0]; v2=[0 0 1 1 1 0 0]; v3=[0 0 0 1 0 0 0]; for j=1:7 if(y<t1(j)) break end end SaT = v1(j); SaB = 1-v1(j); SbT = v2(j); SbB = 1-v2(j); ScT = v3(j); ScB = 1-v3(j);

end

% sector II elseif (x>=pi/3) && (x<2*pi/3)

adv= x-pi/3; tb = mag * sin(pi/3-adv); ta = mag * sin(adv); t0 =(ts-ta-tb);

t1=[t0/4 ta/2 tb/2 t0/2 tb/2 ta/2 t0/4]; t1=cumsum(t1);

v1=[0 0 1 1 1 0 0]; v2=[0 1 1 1 1 1 0]; v3=[0 0 0 1 0 0 0];

for j=1:7 if(y<t1(j)) break end end

SaT = v1(j); SaB = 1-v1(j); SbT = v2(j); SbB = 1-v2(j); ScT = v3(j); ScB = 1-v3(j);

end %sector III

    elseif (x>=2*pi/3) && (x<pi)
        		adv=x-2*pi/3;
                ta = mag * sin(pi/3-adv);
		        tb = mag * sin(adv);
		        t0 =(ts-ta-tb);

t1=[t0/4 ta/2 tb/2 t0/2 tb/2 ta/2 t0/4]; t1=cumsum(t1); v1=[0 0 0 1 0 0 0]; v2=[0 1 1 1 1 1 0]; v3=[0 0 1 1 1 0 0]; for j=1:7 if(y<t1(j)) break end end SaT = v1(j); SaB = 1-v1(j); SbT = v2(j); SbB = 1-v2(j); ScT = v3(j); ScB = 1-v3(j); end

%sector IV

    elseif (x>=-pi) && (x<-2*pi/3)
                adv = x  + pi; 

tb= mag * sin(pi/3 - adv); ta = mag * sin(adv); t0 =(ts-ta-tb);

t1=[t0/4 ta/2 tb/2 t0/2 tb/2 ta/2 t0/4]; t1=cumsum(t1); v1=[0 0 0 1 0 0 0]; v2=[0 0 1 1 1 0 0]; v3=[0 1 1 1 1 1 0];

for j=1:7 if(y<t1(j)) break end end

SaT = v1(j); SaB = 1-v1(j); SbT = v2(j); SbB = 1-v2(j); ScT = v3(j); ScB = 1-v3(j);

end % sector V

    elseif (x>=-2*pi/3) && (x<-pi/3)

adv = x+2*pi/3;

ta = mag * sin(pi/3-adv); tb = mag * sin(adv); t0 =(ts-ta-tb);

t1=[t0/4 ta/2 tb/2 t0/2 tb/2 ta/2 t0/4]; t1=cumsum(t1); v1=[0 0 1 1 1 0 0]; v2=[0 0 0 1 0 0 0]; v3=[0 1 1 1 1 1 0];

for j=1:7 if(y<t1(j)) break end end

SaT = v1(j); SaB = 1-v1(j); SbT = v2(j); SbB = 1-v2(j); ScT = v3(j); ScB = 1-v3(j);

end

%Sector VI (x>=-pi/3) && (x<0)

    else 

adv = x+pi/3; tb = mag * sin(pi/3-adv); ta = mag * sin(adv); t0 =(ts-ta-tb);

t1=[t0/4 ta/2 tb/2 t0/2 tb/2 ta/2 t0/4]; t1=cumsum(t1); v1=[0 1 1 1 1 1 0]; v2=[0 0 0 1 0 0 0]; v3=[0 0 1 1 1 0 0]; for j=1:7 if(y<t1(j)) break end end SaT = v1(j); SaB = 1-v1(j); SbT = v2(j); SbB = 1-v2(j); ScT = v3(j); ScB = 1-v3(j); end

回答(2 个)

Kai Domhardt
Kai Domhardt 2018-5-1
Your control flow is broken, it appears that there is always one "end" when you see three "end"
Also there is no If statement corresponding to all those Elseif statements.
I have deleted all conditionals and actual code, as to leave only the control flow structure, that should make it easier to see what is wrong.
function
% sector I
for
if
end
end
end
% sector II
for
if
end
end
end
%sector III
elseif
for
if
end
end
end
%sector IV
elseif
for
if
end
end
end
% sector V
elseif
for
if
end
end
end
%Sector VI
else
for
if
end
end
end

keerthi vardhana
keerthi vardhana 2018-5-30
STILL I HAD RECEIVED ERROR ?
  1 个评论
Steven Lord
Steven Lord 2018-5-30
If you have the code open in the MATLAB Editor, select all of it, right-click, and select "Smart Indent" from the context menu. This will line up for, if, elseif, and else statements with their corresponding end statements.
The final end should line up with the initial for or if. If it doesn't, you're probably missing one or more end statements. If it does but there are other end statements all at the same level of indentation (with the later ones possibly underlined in red) then you probably have too many end statements.
Basically, look for red Code Analyzer indicators as they will likely indicate where the problem is located (or where MATLAB was able to determine that there is a problem.)

请先登录,再进行评论。

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by