2016-07-25 32 views
-1

我有一個關於在MATLAB中更改繪圖中的線條顏色的問題。我已經編寫了一個代碼,用於更改嵌入幾個for循環中的代碼行中的行的顏色(代碼如下所示)。在代碼中,當「if循環」條件滿足時,會創建一個新的繪圖(使用它自己的圖形)。我希望每個繪圖都具有不同顏色的線條(來自其他繪圖),因此我創建了一個變量=「NewColor」來增加和更改線條顏色。但是,發生以下問題:更改循環中的繪圖線的顏色

假設我處於調試模式,並且已停止在繪圖命令。我運行下一步,用藍線創建一個繪圖。我檢查NewColor的值並找到NewColor = 0.1。接下來,我使用「run to cursor」命令來下一步激活繪圖命令。當我這樣做時,我仍然在「我for循環」,所以NewColor沒有改變。我檢查編輯器以查找NewColor = 0.1。因此,當我運行下一步時,藍線應顯示在當前的圖上。相反,我的懷疑出現了一條橙線(除藍線外)。我不明白,因爲在調試器的兩個步驟中NewColor = 0.1,並且代碼被寫成線的顏色= [0,NewColor,0]。如果任何人都可以找到我的方式錯誤,將不勝感激。由於

ThetaPlot = [40,50]; % Put incident angle as input 

Count1 = 0; 
Count2 = 0; 
NewColor = 0; 

for m = 1:length(ThetaPlot) 
    NewColor = 0.1; 
    Title = sprintf('Angle(%d)',ThetaPlot(m)); 
    figure('name',Title) 
    Count1 = 0; 
    for i = 1:length(xrange)-1    % X Coordinate of Start Node 
     for j = 1:length(yrange)-1   % Y Coordinate of Start Node 
      Count1 = Count1+1; 
      for k = 2:length(xrange)  % X Coordinate of End Node 
       for 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 

全碼:

%% Calculating Angles of Reflection 

run = 1; % Set run = 1 for calculations 

if run == 1 
    xrange = [0:1:14.5]';  % Coordinates to try for Panel Geometry (in) 
    yrange = [0:1:36]';  % Coordinates to try for Panel Geometry (in) 
    ThetaIncident = [-90:1:90]';  % Incident Angle of Ray (measured relative to normal direction with clockwise postive) 
    OvenGlassXrange = [14.5:0.1:36.5]; %Range of X coordinates for 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; 
for i = 1:length(xrange)-1    % X Coordinate of Start Node 
    for j = 1:length(yrange)-1   % Y Coordinate of Start Node 
     Count1 = Count1+1; 
     for k = 2:length(xrange)  % X Coordinate of End Node 
      for l = 2:length(yrange) % Y Coordinate of End Node 
       Count2 = Count2+1; 
       for m = 1:length(ThetaIncident) 
        xStart = xrange(i); 
        yStart = yrange(j); 
        xEnd = xrange(k); 
        yEnd = yrange(l); 

        m1 = (yEnd - yStart)/(xEnd - xStart); % Slope between Start and 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 as input 

Count1 = 0; 
Count2 = 0; 
NewColor = 0; 

for m = 1:length(ThetaPlot) 
    NewColor = 0.1; 
    Title = sprintf('Angle(%d)',ThetaPlot(m)); 
    figure('name',Title) 
    Count1 = 0; 
    for i = 1:length(xrange)-1    % X Coordinate of Start Node 
     for j = 1:length(yrange)-1   % Y Coordinate of Start Node 
      Count1 = Count1+1; 
      for k = 2:length(xrange)  % X Coordinate of End Node 
       hold on; 
       for 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 
+0

您的許多變量是不確定的。請提供[mcve]。 – excaza

+0

請嘗試把'圖;堅持;'在'for'循環之前,刪除循環中的'hold'。如果有效,我會給你寫一個完整的答案 –

+0

請解釋你的'plot'調用。 '[' - ',[0,NewColor,0],'o']'作爲'plot'的參數是沒有意義的。 – excaza

回答

1

,而不是plot(x,y,['-',[0,NewColor,0],'o']);嘗試:

plot(x,y,'linestyle','-','marker','o','color',[0,NewColor,0]) 
+0

此外,我可以建議使用hsv2rgb的升序參數作爲改變'色調'而不是單個r,g或b通道強度的非常好的方法。最近我回答了一個問題,我給出了這樣一個例子:http://stackoverflow.com/questions/38550379/how-to-use-different-color-for-each-vector-in-scatter-function-gnu-octave/ 38557802#38557802 ...我認爲你的顏色變化會更出色:) –

+1

代碼中的這種變化對我有效。謝謝。 –

-1

根據Matlab的文件,通過調用Matlab的hold on使用下一種顏色。

將保留當前座標軸上的座標圖,以便添加到座標軸上的新座標圖不會刪除現有座標圖。新繪圖根據軸的ColorOrder和LineStyleOrder屬性使用下一種顏色和線條樣式。

因此,在您的代碼中,當您在for內調用hold on時,它只使用下一種顏色。

我的解決辦法是把figure; hold on;之前for環和刪除一個在你循環

+2

他試圖通過每個'plot'調用明確地傳遞顏色。 'hold'對軸的下一個顏色的影響是不相關的。此外,反覆調用「hold on」與調用它一次對行爲沒有任何影響。 – excaza