nested if else statements
이전 댓글 표시
Can someone write an example of a nested if else statement? matlab keeps underlining the 'else' when I try to write a nested 'if else' statement telling me the syntax is wrong.
Also, where do I put the 'end' for each of the 'if else' statements?
댓글 수: 6
Jan
2013년 7월 25일
It would be more useful, if you show us your failing code, such that we can suggest a precisely matching solution.
Simone Kolb
2016년 10월 5일
you're probably missing "end" to close the conditional
Rohit Bhagwat
2017년 3월 1일
Yes I have rechecked it many times, the number of 'if' and 'else' statements are perfectly matching, still it shows that one of the if statements does not find a end
Walter Roberson
2017년 3월 1일
Rohit, it is difficult for us to give an informed opinion without seeing the file in question.
BISHMITA SHARMA
2017년 8월 21일
May be u are writing the expression in the next line after 'ifelse' try writing it in the same line... because there will be only one 'end', for the first 'if'
Jan
2017년 8월 21일
@BISHMITA SHARMA: I assume the problem is solved already, because this thread was written 4 years ago.
채택된 답변
추가 답변 (4개)
Nathan Crosty
2013년 7월 25일
편집: Nathan Crosty
2013년 7월 25일
this = 222;
that = 111;
if this == 333
disp('this')
elseif that == 444
disp('that')
else
this = 555;
if this == 222
disp('this')
else
disp('that')
end
end
댓글 수: 1
Because the nested if statement contained within the else segment of the the outer statement has no option for "false" values, this would be more clear to display with an if/elseif statement rather than the nested version. Might be a bit misleading to use it as an example.
Sohrab Abedini
2015년 12월 27일
편집: Sohrab Abedini
2015년 12월 27일
0 개 추천
The examples were great. However, I could not still find a solution to this problem of mine: I don't know how to write it in matlab code->
if n=4 then -> c=2, d=3 or c=3 , d=2.
I wondering if writing "or" is acceptable in result statement. it seems wrong but I have to do it somehow. --- The actual problem is changing a 2nd rank Tensor to a 4th rank tensor through Voigt mapping. which is quite easy vise versa and so difficult in this way. I might be able to solve it if I understand to write "or" or sth like that in if statement.
Nava Subedi
2016년 11월 26일
편집: Stephen23
2016년 11월 26일
Can someone help me to what is wrong with my code:
function [l, m, n] = sort3([a b c])
if a>=b && a>=c
if b>=c
l = c;
elseif b<=c
l = b;
else l = a;
end
elseif b>=a && b>=c
if a>=c
m = c;
elseif a<=c
m = a;
else m = b;
end
else c>=a && c>=b
if a>=b
n = b;
elseif a<=b
n = a;
else n = c;
end
end
I am trying to write nested if statement.
댓글 수: 1
This is not MATLAB syntax:
function [l, m, n] = sort3([a b c])
It looks like you are trying to write Python, or something similar. The correct MATLAB syntax is shown in the online beginners tutorials, the documentation, and lots of online forums:
function [l, m, n] = sort3(a, b, c)
Anyone can read the MATLAB documentation for free. It explains how MATLAB works:
Jobin Geevarghese Thampi
2022년 4월 30일
0 개 추천
what is the wrong with this code?. It is supposed to give 22
댓글 수: 2
Jobin Geevarghese Thampi
2022년 4월 30일

Walter Roberson
2022년 4월 30일
You are showing us the file prac.m but matlab is not going to know to look inside prac.m for the function summa() . You are getting some other summa() function.
카테고리
도움말 센터 및 File Exchange에서 Scripts에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!