Why the elseif condition doesn't work?

i = 1:2071
if (dat(2)==0)&&(dat(3)==0)&&(dat(4)==0)&&(dat(5)==0)
fprintf(fileID,'No cloud');
elseif((dat(2)==0)&&(dat(3)==0)&&(dat(4)==0)&&(dat(5)==1))
fprintf(fileID,'Cirrus');
elseif((dat(2)==0)&&(dat(3)==0)&&(dat(4)==1)&&(dat(5)==0))
fprintf(fileID,'Al_str');
elseif((dat(2)==0)&&(dat(3)==0)&&(dat(4)==1)&&(dat(5)==1))
fprintf(fileID,'Al_cum');
elseif((dat(2)==0)&&(dat(3)==1)&&(dat(4)==0)&&(dat(5)==0))
fprintf(fileID,'St');
elseif((dat(2)==0)&&(dat(3)==1)&&(dat(4)==0)&&(dat(5)==1))
fprintf(fileID,'Sc');
elseif((dat(2)==0)&&(dat(3)==1)&&(dat(4)==1)&&(dat(5)==0))
fprintf(fileID,'Cum');
elseif((dat(2)==0)&&(dat(3)==1)&&(dat(4)==1)&&(dat(5)==1))
fprintf(fileID,'Ns');
else ((dat(2)==1)&&(dat(3)==0)&&(dat(4)==0)&&(dat(5)==0));
fprintf(fileID,'DC \n');
end
end

5 comentarios

Abraham Boayue
Abraham Boayue el 8 de Mzo. de 2018
You defined a for loop without ever using the loop index,i, that is causing trouble for your code already. If you just want to check the values of the data at 2, 3, 4 and 5, then no need for the loop.
Jan
Jan el 8 de Mzo. de 2018
Editada: Jan el 8 de Mzo. de 2018
@Abraham Boayue: Please post this as an answer.
@medha spd: Whenever you write "does not work" in the forum, care for explaining, what the problem is. Do you get an error message or does the behavior differ from your expectations? Currently the readers have to guess, what your problem is. This is not efficient.
Rik
Rik el 8 de Mzo. de 2018
Also, your last line looks like it should be an elseif instead of an else.
David Fletcher
David Fletcher el 8 de Mzo. de 2018
Personally, I'd turn dat(2) - dat(5) into a 4 bit number and use a switch statement instead. All those if, elseif, else statements are a bit of a nightmare
medha spd
medha spd el 9 de Mzo. de 2018
@Jan Simon It was the 1st question that I've asked here.sorry for making it unclear to understand.Will improve :)

Iniciar sesión para comentar.

Respuestas (1)

Jan
Jan el 8 de Mzo. de 2018
While Abraham has posted a useful explanation already, I suggest a simplification of the code in addition:
v = dat(2:5) * [8, 4, 2, 1].';
switch v
case 0
fprintf(fileID,'No cloud');
case 1
fprintf(fileID,'Cirrus');
case 2
fprintf(fileID,'Al_str');
case 3
fprintf(fileID,'Al_cum');
case 4
fprintf(fileID,'St');
case 5
fprintf(fileID,'Sc');
case 6
fprintf(fileID,'Cum');
case 7
fprintf(fileID,'Ns');
otherwise % Or: case 8 ???
fprintf(fileID,'DC \n');
end
By the way: Your last condition was:
else ((dat(2)==1)&&(dat(3)==0)&&(dat(4)==0)&&(dat(5)==0));
This is equivalent to
else
Maybe you meant:
elseif ((dat(2)==1)&&(dat(3)==0)&&(dat(4)==0)&&(dat(5)==0))
? This is a remarkable difference, because else cond evaluates the expression cond, but it has no effect at all.
An even short code:
v = min(8, dat(2:5) * [8, 4, 2, 1].');
p = {'No cloud', 'Cirrus', 'Al_str', 'Al_cum', 'St', 'Sc', 'Cum', 'Ns', 'DC \n'};
fprintf(fileID, p{v + 1});

3 comentarios

medha spd
medha spd el 9 de Mzo. de 2018
Thank you for the answer, but it's not giving the expected output
Rik
Rik el 9 de Mzo. de 2018
You should either explain what is different in your output as expected, or provide an example of an input with the correct output.
Jan
Jan el 9 de Mzo. de 2018
@medha spd: Please consider Rik's comment. I cannot adjust the code to the results you expect, if I do not know, what these expectations are.

Iniciar sesión para comentar.

Categorías

Más información sobre Language Fundamentals en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 8 de Mzo. de 2018

Comentada:

Jan
el 9 de Mzo. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by