Adding a color scale to loop script

조회 수: 5 (최근 30일)
aaron Harvey
aaron Harvey 2015년 10월 12일
댓글: Alexandar 2022년 7월 25일
Hey. So I have a loop script plotting lines ontop of each other on the same figure. Its messy but it works.
function TempSalplot(datafile)
datafile=importdata(datafile);
A=datafile.data;
%depth,distance,salinity,temp are in column 28,2,14,11 of datafile respectively;
for z=[1:2:max(datafile.data(:,28))];
for depthi=[1:length(A(:,28))];
if A(depthi,28)>z-0.5 & A(depthi,28)<z+0.5;
B(depthi,1)=A(depthi,2);
B(depthi,2)=A(depthi,14);
B(depthi,3)=A(depthi,11);
%delete zero rows in new matrix 'B'
B(all(B==0,2),:)=[];
% average repeated measurements for same distances
[C,ia,idx]=unique(B(:,1),'stable');
val=accumarray(idx,B(:,2),[],@mean);
M12=[C val];
[C,ia,idx]=unique(B(:,1),'stable');
val=accumarray(idx,B(:,3),[],@mean);
M13=[C val];
M=[M12,M13(:,2)];
end
end
plot(M(:,2),M(:,3),'.-','color',rand(1,3))
hold on
end
end
I would like it, to rather than plot each line with a random colour, to plot each new line according to a colour scale so I can put a color bar on it. Or at least represent that each new line is increasing depth of 1m. Thanks for any help.
  댓글 수: 2
Walter Roberson
Walter Roberson 2015년 10월 12일
At the time of that plot(), which variable holds the depth that you would like to color code by?
aaron Harvey
aaron Harvey 2015년 10월 12일
편집: aaron Harvey 2015년 10월 12일
It would be z at that point, defined in the beginning of the for loop,
for z=[1:2:max(datafile.data(:,28))];

댓글을 달려면 로그인하십시오.

채택된 답변

Marius
Marius 2015년 10월 12일
Hi Aaron, What I do when I plot many plots in a for-loop (N plots) is first create a "colour matrix" (col) based on a colormap (e.g. 'jet'):
col = jet(N);
for i = 1:N
plot( myXYvals(:,N), 'Color', col(N,:) )
end
Hope this helps, at least giving you an idea... Marius
  댓글 수: 2
aaron Harvey
aaron Harvey 2015년 10월 12일
편집: aaron Harvey 2015년 10월 12일
Thank-you, That worked perfectly! but is there also a way I can add a colorbar to this? labeled with my N values (or z in my code)
Alexandar
Alexandar 2022년 7월 25일
^Can somebody help with the question above on how to display the colorbar. I have already looked at multiple questions for displayinc colorbars but none of them work since I am also inputting this into a forloop.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by