GingerMonster
GingerMonster

Reputation: 3

Matlab legend color incorrect, in a loop of variable size, reading in legend names

I feel like I have read through/attempted every online suggestion about this - perhaps missing the obvious! In Matlab R2013a I am plotting multiple lines from a loop (the number of lines read in each time is variable) and then adding a legend, where the series names are read in from a list. The line colors on the legend do not match the line colors on the plot.

I have tried color maps, get & set commands, putting the legend in and outside the loop, creating a separate loop for the legend etc etc... Please help! The relevant section of the code is below:

%% getting the data....
for i=1:length(files); % for each file in the folder
  FileName = files(i,1).name; % extract the filename
  calfile = files(i,1).name; % lists all filenames for metadata
  a = length(calfile);
  fn(i,1:a) = calfile;
  fid_data(i) = fopen(files(i,1).name,'r'); % open that file
  data = csvread(FileName,0,1,[0 1 15 30]); % 
  diam(i,:) = data(9,:); % puts each channel center diameter into array
  diamerr(i,:) = data(10,:); % channel centre error
end

x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30];

%% Plotting data
h = (figure('name','campaign summary','numbertitle','off'));
set(gca, 'box','on')

for r = 1:i
   plot(x,diam(r,:)); % plots diameters for each flight
   hold all
   errorbar(diam(r,:), diamerr(r,:)); % plots error bars for each flight diameters
end

legend(fn(:,:),'location', 'northwest'); % legend derived from filename list
set(legend,'Interpreter','none'); % stops the underscore from making text subscript

Upvotes: 0

Views: 522

Answers (1)

Molly
Molly

Reputation: 13610

You don't need both plot and errorbar. The errorbar function is drawing over the line plotted earlier in the loop with plot. The legend is then applied to these hidden lines as well as the visible ones. Get rid of plot and use errorbar(x, diam(r,:), diamerr(r,:)) instead.

Upvotes: 0

Related Questions