Legend and graph doesn't have the same color

21 views (last 30 days)
I have a problem with the color in the legend. I'm using a package "RF Utilities V1.2" which I found at http://www.mathworks.com/matlabcentral/fileexchange/22996-rf-utilities-v1-2. I'm using the function citi2s from that package to import data from citi-files.
I have a pretty long script where I have a lot of functions, I just made a shorter version which will work to state the problem here. For instance I have only two lines to plot, which is easy to do without a for-loop. In my original script I have the number of plots will be determined by the number of files the user selects, and therefore I need the for loop. I also need to use hold all to change the color every time.
This is my code:
clear all;
pathname=char(strcat(pwd,'\'))
files = cell(1,2);
files{1,1}=['DATA01.D1'];
files{1,2}=['DATA02.D1'];
number_of_selected_files=length(files);
number_of_measurement_points=201;
%Pre-allocate empty matrices:
S_parameter_matrix=zeros(number_of_selected_files, 4,...
number_of_measurement_points(1));
freq=zeros(number_of_selected_files, number_of_measurement_points);
filename=zeros(number_of_selected_files,1);
%%----------------------- THIS WAY WORKS ---------------------
load('S21.mat')
number_of_selected_files=2;
freq=linspace(1,1.1,201)*10^9;
%%----------------- THIS WAY DOES NOT WORK -------------------
%{
for k=1:number_of_selected_files;
[S_parameter_matrix(k,1,:), S_parameter_matrix(k,2,:), ...
S_parameter_matrix(k,3,:), S_parameter_matrix(k,4,:), ...
freq(k,:)]=citi2s(char(strcat(pathname, files(k))));
end
freq=freq/(1e-6); % GHz
S21=squeeze(S_parameter_matrix(:,3,:));
%}
%%---------------------- REST OF SCRIPT ----------------------
IL=-20*log10(abs(S21)); %Insertion loss
fig1=figure;
hold all
for k=1:number_of_selected_files
[~, name, ext]=fileparts(files{k});
%Create filename-vector without file extension
filename_string{k} = [char(name)];
%Create string-vector with information for legend
legend_string{k}=['Cable ' num2str(k) ': ' char(name)];
plot(freq, IL(k,:))
end
hold off
leg=legend(legend_string, 'location', 'SouthOutside',....
'Interpreter', 'none');
My problem is that if I use the citi2s-function to import the data my plot uses a different colororder than my legend does. The legend follows the default color order but the plot doesn't. It looks like this:
If I just load the data from the S21.mat-file the color is fine, like this:
Does anyone know why the way of importing the data can affect the color-order in the plot? Or a way to fix it? I'm attaching a zip-file with the script, citi2s, data-files and the S21.mat
Thank you in advance
  3 Comments
Björn
Björn on 8 Aug 2014
Thanks. It's solved now but I appreciate the headsup with hold all. I have always used it like this
plot(x,y); hold all
Without hold off. But now when I had this problem I googled a lot for hold all and found many examples where they did like this. So this is the first time I wrote it like this, and thanks to your comment it's also the last :-).

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 8 Aug 2014
Björn - I was able to reproduce the problem with your code (very helpful to post all that). The problem occurs during the plotting, at the line of code
plot(freq, IL(k,:))
IL is a 2x201 matrix, which is fine since k varies from 1 to 2. But, freq is a 2x201 matrix as well! So we are actually plotting the same data twice, and so are using four of the colours (as the second plot from each iteration just overlays the previous one).
Just replace the above line with
plot(freq(k,:), IL(k,:));
and it should work fine.
  1 Comment
Björn
Björn on 8 Aug 2014
Nice catch. Thank you very much!
It's pretty obvious now when you've pointed it out :-)

Sign in to comment.

More Answers (1)

Chad Greene
Chad Greene on 7 Aug 2014
You can link labels directly to their data with label. Labels take on the color of the data by default.

Community Treasure Hunt

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

Start Hunting!