Colorbar not matching for loop plot colours

7 views (last 30 days)
Hi there i have a for loop plotting multiple lines on the same graph, with the help of @Marius I have managed to get each line a different color in a color scheme (jet). However how can I add a colorbar to the plot? I have tried myself right at the end of the script but its not working?
function TempSalplot(datafile)
datafile=importdata(datafile);
A=datafile.data; %depth,distance,salinity,temp are in column 28,2,14,11 of datafile respectively;
figure;
for z=[1:3:max(datafile.data(:,28))]; %max is around 16
col = jet(z);
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);
B(all(B==0,2),:)=[];
[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',col(z,:));
xlabel('Salinity (psu)');
ylabel('Tempurature (degC)');
hold on
end
h=colorbar('ticks',[1:max(z)]); %This doesn't seem to be working?
caxis([1,max(z)]);
end
The colors in the colorbar just dont match the colors on the graph? Print screen of figure -->http://prntscr.com/8qecy1

Accepted Answer

Ingrid
Ingrid on 12 Oct 2015
just a quick glance so not sure if this is what you are looking for but you should put the call to create the colors of jet outside of your for-loop
figure;
col = jet(max(datafile.data(:,28)));
for z=[1:3:max(datafile.data(:,28))]; %max is around 16
  1 Comment
aaron Harvey
aaron Harvey on 12 Oct 2015
Thank-you. I have to round to an integer but that did the trick.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!