Problem with multiple if conditions....

73 views (last 30 days)
Jules Ray on 15 Sep 2014
Edited: per isakson on 5 Sep 2015
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
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%
if l(1,2)>=l(1,1) && l(2,1)>=l(2,2) %situation 2
a=2
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%
if l(1,2)>=l(1,1) && l(2,2)>=l(2,1) %situation 3
a=3
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
Jules Ray on 16 Sep 2014
Thanks Adam, i did it in your way with an extra modification and it works now... the analysis using the l and its behavior is simply too weird, instead i used the difference between the elements of the matrix array and works perfectly. Check the last lines.
Thanx
Here is the complete code, it reads a table with utm coordinates, then project these coordinates along a profile obtaining a plot:
type of data in the table:
ANTI 1 1 640772 5786590 1057.66 37.08 1.31 classic 735560.77881
ANTI 1 2 641584 5786571 1870.49 133.37 2.65 classic 735560.77924
ANTI 2 1 641108 5785042 899.36 43.10 4.30 classic 735560.77969
CONI 3 1 734515 6087409 798.25 45.00 0.38 classic 735566.41527
CONI 2 1 737715 6087097 1359.38 50.00 0.81 classic 735566.41698
CONI 2 2 737374 6087613 1977.23 75.00 1.10 classic 735566.41894
CONI 4 2 733623 6086157 422.26 62.07 2.79 classic 735566.41973
and this is the script:
%projection of shorelines routines
file=('shorelines.txt');
if exist(file,'file')==1 || exist(file,'file')==2
fid=fopen(file,'r');
i=1;
while ~feof(fid)
%tline = fgetl(fid);
A=textscan( fid,'%s %u %u %f %f %f %f %f %s %f/' ...
for j=1:10 %number of columns (default)
data(i,j) = A(1,j);
end
i=i+1;
end
fclose(fid);
%extract data from table
nim=numel(data(:,10));
u=1;
for k=1:nim
md1(u,1)=data{k,4}; %x
md1(u,2)=data{k,5}; %Y
md1(u,3)=data{k,7}; %z
md1(u,4)=data{k,8}; %ze
md1(u,5)=data{k,3}; %shore
u=1+u;
end
end
% set variables
x=md1(:,1);
y=md1(:,2);
z=md1(:,3);
ze=md1(:,4);
shore=md1(:,5);
figure(1) %input figure
clf
hold on
scatter(x,y,75,z,'filled');
col=colorbar;
tite = get(col,'title');
set(tite,'string','Elevation (m)');
axis equal
box on
ylabel('UTM N')
xlabel('UTM E')
%graphic input
[xl,yl]=ginput(2);
l(:,1)=xl;
l(:,2)=yl;
plot(xl,yl,'-k') %plot of profile
plot(l(1,1),l(1,2),'or'); %point1
plot(l(2,1),l(2,2),'ob'); %point2
dx=l(1,1)-l(2,1);
dy=l(1,2)-l(2,2);
m=(dy/dx);
m2=-1/m;
an=-atan(m)*180/pi;
anr=an*pi/180;
ddg=sqrt((l(1,1)-l(2,1))^2+(l(1,2)-l(2,2))^2);
dkm=(ddg);
b=l(1,2)-m*l(1,1);
%text in plot
str1=num2str(dkm/1000); %m to km
text( min(xl)+(dx/2),min(yl)+(dy/2) ...
, sprintf('%s km',str1),'FontSize',12)
%rotate and reproject data
for i=1:length(md1(:,1))
b2=md1(i,2)-m2*md1(i,1);
Pnx=(b2-b)/(m-m2);
Pny=m*Pnx+b;
Pn=[Pnx Pny];
dp(i,1)=Pn(1,1); %xp
dp(i,2)=Pn(1,2); %yp
dp(i,3)=((l(1,2)-dp(i,2))/sin(anr)); % distance in km
dp(i,4)=md1(i,3);
dp(i,5)=md1(i,4);
end
% %%%%%%%figure B/N of shorelines
figure(2)
clf
subplot(2,1,1)
hold on
box on
%loop shorelines colors
for i=min(shore):max(shore)
plot( dp(shore==i,3)/1000,dp(shore==i,4),'.' ...
, 'color',[1/i 1/i 1/i],'markersize',20)
j(i,1)=i;
end
%cartesian plane conditions for profiles
if dx>0 && dy>0
xlim([-dkm/1000 0])
a=1; %a is a parameter for data export
elseif dx<0 && dy<0
xlim([0 dkm/1000])
a=3;
elseif dx>0 && dy<0
xlim([-dkm/1000 0])
a=4;
else
a=2;
xlim([0 dkm/1000])
end
%legend
leg=legend(num2str(j),'location','eastoutside');
tit = get(leg,'title');
set(tit,'string','Shorelines');
%plot errorbars
errorbar(dp(:,3)/1000,dp(:,4),(dp(:,5)),'ok')
ylabel('Elevation (m)')
xlabel('Distance along profile (km)')
% save routine

David Young on 16 Sep 2014
Edited: David Young on 16 Sep 2014
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?
Jules Ray on 16 Sep 2014
thanx, instead of use the matrix l, i use the difference between their elements and work properly now.