matlab - Changing color of plot lines in a loop -
i have question changing color of lines in plot in matlab. have written code want change color of lines in plot embedded in several loops (the code shown below). in code, new plot created (with own figure) when "if loop" condition satisfied. want each plot have lines different colors (from other plots), created variable = "newcolor" increment , change line colors. however, following problem has been occurring:
suppose in debug mode , have stopped @ plot command. run next step , plot created blue line. check value of newcolor , find newcolor = 0.1. next, use "run cursor" command step next time plot command activated. when still within "i loop", newcolor has not changed. check editor find newcolor = 0.1. therefore, when run next step blue line should show on current plot. contrary , disbelief orange line shows (in addition blue line). don't understand since in both steps of debugger newcolor = 0.1, , code written color of lines = [0,newcolor,0]. if can find error of ways appreciated.
thetaplot = [40,50]; % put incident angle input count1 = 0; count2 = 0; newcolor = 0; m = 1:length(thetaplot) newcolor = 0.1; title = sprintf('angle(%d)',thetaplot(m)); figure('name',title) count1 = 0; = 1:length(xrange)-1 % x coordinate of start node j = 1:length(yrange)-1 % y coordinate of start node count1 = count1+1; k = 2:length(xrange) % x coordinate of end node l = 2:length(yrange) % y coordinate of end node count2 = count2+1; if reflraydata(count2,thetaplot(m) - thetaincident(1) + 1,count1) == 1 x = [xrange(i),xrange(k)]; y = [yrange(j),yrange(l)]; plot(x,y,['-',[0,newcolor,0],'o']); hold on; end end count2 = 0; end end end newcolor = newcolor + 0.02; end full code:
%% calculating angles of reflection run = 1; % set run = 1 calculations if run == 1 xrange = [0:1:14.5]'; % coordinates try panel geometry (in) yrange = [0:1:36]'; % coordinates try panel geometry (in) thetaincident = [-90:1:90]'; % incident angle of ray (measured relative normal direction clockwise postive) ovenglassxrange = [14.5:0.1:36.5]; %range of x coordinates oven glass reflraydata = zeros((length(xrange)-1)*(length(yrange)-1),length(thetaincident),(length(xrange)-1)*(length(yrange)-1)); % matrix containing reflected ray data count1 = 0; count2 = 0; = 1:length(xrange)-1 % x coordinate of start node j = 1:length(yrange)-1 % y coordinate of start node count1 = count1+1; k = 2:length(xrange) % x coordinate of end node l = 2:length(yrange) % y coordinate of end node count2 = count2+1; m = 1:length(thetaincident) xstart = xrange(i); ystart = yrange(j); xend = xrange(k); yend = yrange(l); m1 = (yend - ystart)/(xend - xstart); % slope between start , end nodes b1 = ystart - m1*xstart; m2 = 1/m1; % slope of normal direction b2 = (yend - 0.5*(yend - ystart)) - m2*(xend - 0.5*(xend - xstart)); arbxcoor = 1; % arbitary point x coordinate on normal line arbycoor = m2*arbxcoor+b2; % arbitary point y coordinate on normal line thetareflected = -thetaincident(m); % reflected angle arbxcoorrot = arbxcoor*cosd(thetareflected) - arbycoor*sind(thetareflected); % arbitary point x coordinate on reflected line arbycoorrot = arbycoor*cosd(thetareflected) + arbxcoor*sind(thetareflected); % arbitary point y coordinate on reflected line m3 = (arbycoorrot - (yend - 0.5*(yend - ystart)))/(arbxcoorrot - (xend - 0.5*(xend - xstart))); % slope of reflected line b3 = (yend - 0.5*(yend - ystart)) - m3*(xend - 0.5*(xend - xstart)); elemlength = sqrt((yend - ystart)^2 + (xend - xstart)^2); if min(ovenglassxrange) < -b3/m3 && -b3/m3 < max(ovenglassxrange) && -1 < m1 && m1 < 0 && m1 ~= -inf && m1 ~= inf && elemlength < 3 reflraydata(count2,m,count1) = 1; end end end end count2 = 0; end end %% plotting thetaplot = [40,50]; % put incident angle input count1 = 0; count2 = 0; newcolor = 0; m = 1:length(thetaplot) newcolor = 0.1; title = sprintf('angle(%d)',thetaplot(m)); figure('name',title) count1 = 0; = 1:length(xrange)-1 % x coordinate of start node j = 1:length(yrange)-1 % y coordinate of start node count1 = count1+1; k = 2:length(xrange) % x coordinate of end node hold on; l = 2:length(yrange) % y coordinate of end node count2 = count2+1; if reflraydata(count2,thetaplot(m) - thetaincident(1) + 1,count1) == 1 x = [xrange(i),xrange(k)]; y = [yrange(j),yrange(l)]; plot(x,y,['-',[0,newcolor,0],'o']); hold on; end end count2 = 0; end end end newcolor = newcolor + 0.02; end
instead of plot(x,y,['-',[0,newcolor,0],'o']); try:
plot(x,y,'linestyle','-','marker','o','color',[0,newcolor,0])
Comments
Post a Comment