Adding legend in a plot genereted by a loop

1,665 views (last 30 days)
Hello everyone
i have a lot of x,y data sets that i would like to plot. The data sets are stored in a cell structure so that
  • DataFiles(1,1) is the first data set i imported
  • DataFiles(1,2) is the second
  • DataFiles(1,1).data(:,2) is all the y values of the first data set
and so on. the names of the data sets are furthermore stored in OutFiles so that
  • OutFiles(1,1).name is the name of the first data set
  • OutFiles(2,1).name is the name of the second data set
and so on. because i would like to plot all the x,y data set togehter i wrote a loop. the code is shown below
figure( 'Name', 'Reduced data' );
title('Reduced Raw data')
cc = jet(length(OutFiles)); % creates colormap
% log-lin plot
subplot(2,1,1)
h1 = subplot(2,1,1);
set(h1, 'xscale', 'lin')
set(h1, 'yscale', 'log')
xlabel('Q (Å^{-1})','fontsize',14)
ylabel('Detector counts in 1 sec.')
hold on
for k = 1:length(OutFiles)
plot(DataFiles{1,k}.data(:,1),DataFiles{1,k}.data(:,2), 'color', cc(k,:));
end
+ the other subplot which is just a log-log plot of the same
My problem is that i cannot figure out how i can include the legends in the plot. i have tried include "legend" in the loop so that the loop code becomes
for k = 1:length(OutFiles)
plot(DataFiles{1,k}.data(:,1),DataFiles{1,k}.data(:,2), 'color', cc(k,:));
legend(OutFiles(k,1)
end
but this does not work. Simply adding legend(OutFiles) also does not work but OutFiles also contains more information than ".name". i thought about making an array of the names "Legend(1,length(OutFiles))" but i do not know how to make an array of strings. i tried using
>> Legend=zeros(1,10); >> Legend(1,8) = 'test' Subscripted assignment dimension mismatch.
but as you can see i cannot add a string to an array
does anyone have a suggestion on how to add legends to plots made using a loop? any help will be greatly appreciated :)
  2 Comments
Amani mania
Amani mania on 24 Nov 2021
Translate & rotate:
I = imread('img.jpg');
J = imtranslate(I,[35, 50]);
S = imrotate(J,35,'bilinear');
figure
subplot(1,3,1)
imshow(I)
subplot(1,3,2)
imshow(J)
subplot(1,3,3)
imshow(S)
Rotation and Scale:
I = imread('img.jpg');
Y = imresize(I, [512 512]);
Z = imresize(Y, [128 128]);
S = imrotate(Z,35,'bilinear');
figure subplot(1,4,1)
imshow(I)
subplot(1,4,2)
imshow(Y)
subplot(1,4,3)
imshow(Z)
subplot(1,4,4)
imshow(S)
Translate & Scale:
I = imread('img.jpg');
Y = imresize(I, [512 512]);
Z = imresize(Y, [128 128]);
S = imtranslate(I,[35, 50]);
figure
subplot(1,4,1)
imshow(I)
subplot(1,4,2)
imshow(Y)
subplot(1,4,3)
imshow(Z)
subplot(1,4,4)
imshow(S)

Sign in to comment.

Accepted Answer

Youssef  Khmou
Youssef Khmou on 6 Feb 2013
hi, you plot all the data first, and then manipulate the Legend using per example cells as shown below :
for k = 1:length(OutFiles)
plot(DataFiles{1,k}.data(:,1),DataFiles{1,k}.data(:,2), 'color', cc(k,:));
end
Legend=cell(2,1)% two positions
Legend{1}=' Your data 1' ;
Legend{2}=' Your data 2';
legend(Legend);
In case there are many legends, like N plots, then you do :
Legend=cell(N,1)
for iter=1:N
Legend{iter}=strcat('Your_Data number', num2str(iter));
end
legend(Legend)
  8 Comments
sanjay Negi
sanjay Negi on 26 Oct 2021
Its a great help changing color at each iteration. Thanks

Sign in to comment.

More Answers (1)

Samuel Hammarberg
Samuel Hammarberg on 30 May 2019
When I loop through folders etc. containing data I want to plot, I usually use the 'DisplayName' option.
Example of what my plotscripts typically includes.
% This script plots force vs displacement stored in the Data struct.
figure, hold on
h = legend('show','location','best');
set(h,'FontSize',12);
for i=1:length(Data) % Data is a struct containing my data.
x = Data(i).displacement;
y = Data(i).force;
name = Data(i).name; % Some text string which i usually take from the folder of each data set in Data.
plot(x,y,'-x','LineWidth',2,'MarkerSize',2,'DisplayName',name)
end

Community Treasure Hunt

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

Start Hunting!