How to write multiple if statements
223 次查看(过去 30 天)
显示 更早的评论
Can i write more than 3 if statements?
if i can why did i get this error?!
??? elseif source1(i,j)==3;
|
Error: Illegal use of reserved keyword "elseif".
this happens after i use
if source1(i,j)==1;
and
elseif source1(i,j)==2;
plus i want to use if condition for seven times!
2 个评论
Kevin Claytor
2012-7-24
Can you post more of the code, there may be a line before the elseif that is causing the error.
Jan
2012-7-25
Arman has posted the code and accepted it by accident. I've deleted this answer and moved the code to the comment section, because it is not useful to explain the actual problem.
source1=csvread('C:\Users\Arman\Desktop\Matlab\CSVread\1st source_grid_csv.csv');
site=[60.5,31.5];
Mmin=4;
DM=0.1;
c1=-1.09;
c2=0.238;
c3=-1;
c4=-0.0005;
sigma=0.15;
sumNy=zeros(1,1.5/0.01);
CellArea=0.01*112.2*0.01*96.3;
for i=1:521
for j=1:400
if source(i,j)~=-9999;
Celldecimal=[(57.935+(j-1)*0.01),(34.108-(i-1)*0.01)];
R= acos(sin(site(1,2)*pi/180)*sin(Celldecimal(1,2)*pi/180)+cos(site(1,2)*pi/180)*cos(Celldecimal(1,2)*pi/180)*cos((Celldecimal(1,1)-site(1,1))*pi/180))*6371;
if source1(i,j)==1;
a=-1.6739;
b=0.6531;
Mmax=7.8;
Davg=17;
beta=b*log(10);
NM0=10^(a)*CellArea;
NMmin=NM0*(exp(-beta*Mmin)-exp(-beta*Mmax))/(1-exp(-beta*Mmax));
r=sqrt(R^(2)+Davg^(2));
for PGA=0.01:0.01:1.5
Nymatrix=zeros((7.9-Mmin+DM)/DM,1);
for aa=Mmin:0.1:Mmax
Mmean=aa+0.05;
Freq=NMmin*(exp(-beta*(aa-Mmin))-exp(-beta*((aa+0.1)-Mmin)))/(1-exp(-beta*(Mmax-Mmin)));
loga=log10(PGA);
logamean=c1+c2*Mmean+c3*log10(r)+c4*r;
lognormal=1-normcdf(loga,logamean,sigma);
Ny=Freq*lognormal;
if aa==Mmin
Nymatrix((aa-Mmin+DM)/DM,1)=Ny;
else
Nymatrix(round((aa-Mmin+DM)/DM),1)=Ny+Nymatrix(round((aa-Mmin)/DM),1);
end
end
sumNy(1,round(PGA/0.01))=sum(Nymatrix,1)+sumNy(1,round(PGA/0.01));
end
end
elseif source1(i,j)==2;
a=-0.2525;
b=1.1351;
Mmax=5.7;
Davg=16;
beta=b*log(10);
beta=b*log(10);
NM0=10^(a)*CellArea;
NMmin=NM0*(exp(-beta*Mmin)-exp(-beta*Mmax))/(1-exp(-beta*Mmax));
r=sqrt(R^(2)+Davg^(2));
for PGA=0.01:0.01:1.5
Nymatrix=zeros((7.9-Mmin+DM)/DM,1);
for aa=Mmin:0.1:Mmax
Mmean=aa+0.05;
Freq=NMmin*(exp(-beta*(aa-Mmin))-exp(-beta*((aa+0.1)-Mmin)))/(1-exp(-beta*(Mmax-Mmin)));
loga=log10(PGA);
logamean=c1+c2*Mmean+c3*log10(r)+c4*r;
lognormal=1-normcdf(loga,logamean,sigma);
Ny=Freq*lognormal;
if aa==Mmin
Nymatrix((aa-Mmin+DM)/DM,1)=Ny;
else
Nymatrix(round((aa-Mmin+DM)/DM),1)=Ny+Nymatrix(round((aa-Mmin)/DM),1);
end
end
sumNy(1,round(PGA/0.01))=sum(Nymatrix,1)+sumNy(1,round(PGA/0.01));
end
end
elseif source1(i,j)==3;
a=-1.2474;
b=0.8548;
Mmax=6.3;
Davg=20;
beta=b*log(10);
NM0=10^(a)*CellArea;
NMmin=NM0*(exp(-beta*Mmin)-exp(-beta*Mmax))/(1-exp(-beta*Mmax));
r=sqrt(R^(2)+Davg^(2));
for PGA=0.01:0.01:1.5
Nymatrix=zeros((7.9-Mmin+DM)/DM,1);
for aa=Mmin:0.1:Mmax
Mmean=aa+0.05;
Freq=NMmin*(exp(-beta*(aa-Mmin))-exp(-beta*((aa+0.1)-Mmin)))/(1-exp(-beta*(Mmax-Mmin)));
loga=log10(PGA);
logamean=c1+c2*Mmean+c3*log10(r)+c4*r;
lognormal=1-normcdf(loga,logamean,sigma);
Ny=Freq*lognormal;
if aa==Mmin
Nymatrix((aa-Mmin+DM)/DM,1)=Ny;
else
Nymatrix(round((aa-Mmin+DM)/DM),1)=Ny+Nymatrix(round((aa-Mmin)/DM),1);
end
end
sumNy(1,round(PGA/0.01))=sum(Nymatrix,1)+sumNy(1,round(PGA/0.01));
end
end
elseif source1(i,j)==4;
a=-1.1237;
b=0.8127;
Mmax=7.1;
Davg=18;
beta=b*log(10);
NM0=10^(a)*CellArea;
NMmin=NM0*(exp(-beta*Mmin)-exp(-beta*Mmax))/(1-exp(-beta*Mmax));
r=sqrt(R^(2)+Davg^(2));
for PGA=0.01:0.01:1.5
Nymatrix=zeros((7.9-Mmin+DM)/DM,1);
for aa=Mmin:0.1:Mmax
Mmean=aa+0.05;
Freq=NMmin*(exp(-beta*(aa-Mmin))-exp(-beta*((aa+0.1)-Mmin)))/(1-exp(-beta*(Mmax-Mmin)));
loga=log10(PGA);
logamean=c1+c2*Mmean+c3*log10(r)+c4*r;
lognormal=1-normcdf(loga,logamean,sigma);
Ny=Freq*lognormal;
if aa==Mmin
Nymatrix((aa-Mmin+DM)/DM,1)=Ny;
else
Nymatrix(round((aa-Mmin+DM)/DM),1)=Ny+Nymatrix(round((aa-Mmin)/DM),1);
end
end
sumNy(1,round(PGA/0.01))=sum(Nymatrix,1)+sumNy(1,round(PGA/0.01));
end
end
elseif source1(i,j)==5;
a=-1.4385;
b=0.7444;
Mmax=7;
Davg=19;
beta=b*log(10);
NM0=10^(a)*CellArea;
NMmin=NM0*(exp(-beta*Mmin)-exp(-beta*Mmax))/(1-exp(-beta*Mmax));
r=sqrt(R^(2)+Davg^(2));
for PGA=0.01:0.01:1.5
Nymatrix=zeros((7.9-Mmin+DM)/DM,1);
for aa=Mmin:0.1:Mmax
Mmean=aa+0.05;
Freq=NMmin*(exp(-beta*(aa-Mmin))-exp(-beta*((aa+0.1)-Mmin)))/(1-exp(-beta*(Mmax-Mmin)));
loga=log10(PGA);
logamean=c1+c2*Mmean+c3*log10(r)+c4*r;
lognormal=1-normcdf(loga,logamean,sigma);
Ny=Freq*lognormal;
if aa==Mmin
Nymatrix((aa-Mmin+DM)/DM,1)=Ny;
else
Nymatrix(round((aa-Mmin+DM)/DM),1)=Ny+Nymatrix(round((aa-Mmin)/DM),1);
end
end
sumNy(1,round(PGA/0.01))=sum(Nymatrix,1)+sumNy(1,round(PGA/0.01));
end
end
elseif source1(i,j)==6;
a=-1.6544;
b=0.6865;
Mmax=7.8;
Davg=23;
beta=b*log(10);
NM0=10^(a)*CellArea;
NMmin=NM0*(exp(-beta*Mmin)-exp(-beta*Mmax))/(1-exp(-beta*Mmax));
r=sqrt(R^(2)+Davg^(2));
for PGA=0.01:0.01:1.5
Nymatrix=zeros((7.9-Mmin+DM)/DM,1);
for aa=Mmin:0.1:Mmax
Mmean=aa+0.05;
Freq=NMmin*(exp(-beta*(aa-Mmin))-exp(-beta*((aa+0.1)-Mmin)))/(1-exp(-beta*(Mmax-Mmin)));
loga=log10(PGA);
logamean=c1+c2*Mmean+c3*log10(r)+c4*r;
lognormal=1-normcdf(loga,logamean,sigma);
Ny=Freq*lognormal;
if aa==Mmin
Nymatrix((aa-Mmin+DM)/DM,1)=Ny;
else
Nymatrix(round((aa-Mmin+DM)/DM),1)=Ny+Nymatrix(round((aa-Mmin)/DM),1);
end
end
sumNy(1,round(PGA/0.01))=sum(Nymatrix,1)+sumNy(1,round(PGA/0.01));
end
end
elseif source1(i,j)==7;
a=-0.9803;
b=0.8746;
Mmax=6.2;
Davg=22;
beta=b*log(10);
NM0=10^(a)*CellArea;
NMmin=NM0*(exp(-beta*Mmin)-exp(-beta*Mmax))/(1-exp(-beta*Mmax));
r=sqrt(R^(2)+Davg^(2));
for PGA=0.01:0.01:1.5
Nymatrix=zeros((7.9-Mmin+DM)/DM,1);
for aa=Mmin:0.1:Mmax
Mmean=aa+0.05;
Freq=NMmin*(exp(-beta*(aa-Mmin))-exp(-beta*((aa+0.1)-Mmin)))/(1-exp(-beta*(Mmax-Mmin)));
loga=log10(PGA);
logamean=c1+c2*Mmean+c3*log10(r)+c4*r;
lognormal=1-normcdf(loga,logamean,sigma);
Ny=Freq*lognormal;
if aa==Mmin
Nymatrix((aa-Mmin+DM)/DM,1)=Ny;
else
Nymatrix(round((aa-Mmin+DM)/DM),1)=Ny+Nymatrix(round((aa-Mmin)/DM),1);
end
end
sumNy(1,round(PGA/0.01))=sum(Nymatrix,1)+sumNy(1,round(PGA/0.01));
end
end
end
end
end
Y=0.01:0.01:1.5;
loglog(sumNy,Y);
axis([.0001 .01 .01 1.5])
采纳的回答
C.J. Harris
2012-7-24
编辑:C.J. Harris
2012-7-24
You have too many 'end' statements within your code. An if-else statement should be formatted like this:
if a == 1
% Code
elseif a == 2
% Code
elseif a == 3
% Code
else
% Code
end
In your code however you appear to have put an 'end' before each 'elseif' statement which is resulting in the error you are seeing.
更多回答(2 个)
Leah
2012-7-24
looks like you have an end before all your elseif's that shouldn't be there. It helps to smart indent your code when doing nested if's. Press Ctrl+A (select all) then Ctrl+I (smart indent). The structure of your code seems to be
if source(i,j)==2
% bunch of code
else if source(i,j)==3
% bunch of code
else if source(i,j)=4
% bunch of code
end
end
end
Notice all of the end go at the bottom. To clean this up you might want to use a switch case statement. which would look like this
switch source(i,j)
case 2
% bunch of code
case 3
% bunch of code
case 4
% bunch of code
case 9999
% bunch of code
case otherwise
% bunch of code
end
0 个评论
Goh XinYee
2019-4-1
nono, it is that there is a difference between
else if
and
elseif
...
it is not correct to type else if, as with every 'if', we need an 'end', which is why the program won't run with only one 'end'.
if
else if
else if
end
end
end
however, it is not that case with 'elseif', there is only one 'if' with many 'elseif' (s), and thus only one 'end' is needed.
if
elseif
elseif
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!