MATLAB - Add legend next to some chosen plots in a subplot - for loop
-
15-06-2021 - |
Question
I want to add legends in a subplot, bu only for certain plots. Here is my code :
for j = 1:length(FL)
for i = 1:length(index_list)
pos=pos+1;
subplot(size(FL,1),length(index_list), pos)
legend(num2str(ms_list(i)), 'Location', 'NorthOutside');
imagesc(imread(FL{j,:},index_list(i)))
if i==1
legend(FL(j),'Location', 'WestOutside')
end
end
The subplot contains frames extracted from multiframes .tif files. Indexes of the wanted frames are in index_list (the columns). Path to the files wanted are in FL (the rows). What I want to add on the figure is the name of the file at the left of each row and the frame index for each image plotted. ms_list contains the equivalent in milliseconds of the indexes, thats actually what I want to show. Doing like that returns "Plot empty" at each passage in the loop.
Any idea ?
Thanks
JC
Solution
From your description and code, it seems that legend
isn't what you want; rather, you want a title (above the plot) and ylabel (left of certain plots). legend
is to give labels to particular objects inside the plot, such as a line series for example.
for j = 1:length(FL)
for i = 1:length(index_list)
pos=pos+1;
subplot(size(FL,1),length(index_list), pos)
title(num2str(ms_list(i))); %#<---title here
imagesc(imread(FL{j,:},index_list(i)))
if i==1
ylabel(FL(j)) %#<---ylabel here
end
end
end
The reason you were getting an error is that you were applying legend
to an empty set of axes. legend
labels the children of axes; no children, nothing to label, hence the error.