Problem with multiple if conditions....
5 次查看(过去 30 天)
显示 更早的评论
Hello there:
I have a code composed of 4 if, this are the condition for the xlim of plots that follow the condition. There are 4 situations that i name a=1, a=2, a=3, a=4. Each satisfy certain characteristics of the 2x2 matrix l, which is a line formed by two points :
figure(2)
clf
subplot(2,1,1)
hold on
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%ANGULAR CONDITIONS in the cartesian plane for a line (l)
for i=min(shore):max(shore)
plot(dp(shore==i,3),dp(shore==i,4),'.','color',[1/i 1/i 1/i],'markersize',20)
j(i,1)=i;
end
%%%%%%%%%%%%%%%%%%%%%
if l(1,1)>l(1,2) && l(2,1)>l(2,2) %situation 1
a=1
xlim([0 dkm]) %positive quadrant
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%
if l(1,2)>=l(1,1) && l(2,1)>=l(2,2) %situation 2
a=2
xlim([-dkm 0]) %negative quadrant
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%
if l(1,2)>=l(1,1) && l(2,2)>=l(2,1) %situation 3
a=3
xlim([0 dkm]) %positive quadrant
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if l(1,1)>=l(1,2) && l(2,2)>=l(2,1) %situation 4
a=4
xlim([-dkm 0])
end
legend(num2str(j),'location','eastoutside')
errorbar(dp(:,3),dp(:,4),(dp(:,5)),'ok')%,'o','color',[1/10*i 1/10*i 1/i],'markersize',20)
box on
ylabel('elevation')
xlabel('along profile distance'
however, the if conditions doesn't work properly, in all the runs, only the a=3 condition is used, even if this not satisfy the condition. This is not logic. How can i do to write well this code?. Should i nest the if's?
any help or idea is appreciated... cheers
4 个评论
采纳的回答
David Young
2014-9-16
编辑:David Young
2014-9-16
The conditions are almost mutually exclusive and almost correct. To be absolutely right, you need the second inequality in condition 2 and the first inequality in condition 4 to be > rather than >=. However this detail is unlikely to be causing your problem.
Nesting the if statements would not be helpful. Using elseif as suggested in Adam's comment would be good practice, as it makes the intention more explicit and is very slightly more efficient, but will make little difference to the behaviour (and will make no difference if you make the small corrections suggested above).
The problem is more likely to be in setting the value of the matrix l. You don't show the code that does that. I suggest that you look there, and also print out the value of l at the start of the conditional code, so that you can check what it is. I see that you plot values of dp - maybe l needs to be computed from dp somehow?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Axis Labels 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!