How can I use a legend for only only line in a subpot?

1 view (last 30 days)
Iris Hagoort
Iris Hagoort on 3 May 2018
Answered: Julian Hapke on 3 May 2018
My struct contains 4 conditions and 4 bins. Every subplot is one bin and in every subplot there are lines for all four conditions. In the legend I want to show the four conditions. The script below worked fine when I only plotted this line:
plot(Time',InSecondsAllParticipants.(namesConditions{k}).(namesBins{l}) , 'color' , myCol(k,:));
However, I also want to plot mean+standard deviation and mean-standard deviation. The legend is not correct anymore. Does anyone know how to change it?
myCol = [1 0 0; 0 1 0; 0 0 1; 1 0 1 ];
for l = 1:length(namesBins);
for k = 1:length(namesConditions);
hold on;
plot(Time',InSecondsAllParticipants.(namesConditions{k}).(namesBins{l}) , 'color' , myCol(k,:));
plot(Time',InSecondsAllParticipants.(namesConditions{k}).(namesBins{l}) + ...
StdInSecondsAllParticipants.(namesConditions{k}).(namesBins{l}), ':', 'color' , myCol(k,:));
plot(Time',InSecondsAllParticipants.(namesConditions{k}).(namesBins{l}) - ...
StdInSecondsAllParticipants.(namesConditions{k}).(namesBins{l}), ':', 'color' , myCol(k,:));
xlabel('Time (s)')
ylabel('Concentration OxyHb (M/l)')

Answers (1)

Julian Hapke
Julian Hapke on 3 May 2018
the legend is correct for what you plotted, each iteration creates 3 plots, the namesConditions has 4 entries, which are assigned to the first 4 plots matlab finds in the axes, so your first plot has the correct entry and the next two do not have the entries you want.
You could plot mean and std after creating the legend, to that the 4 conditions are plotted first and linked to the 4 legend entries you are creating.
Or you could use the
syntax of the command and pass the plot handles of the condition plots into the call by collecting them in the loop.
p(k) = plot(Time',InSecondsAllParticipants.(namesConditions{k}).(namesBins{l}) , 'color' , myCol(k,:));

Community Treasure Hunt

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

Start Hunting!