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 onfor 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 🙂
Best Answer