corresponding legends for each curves in one plot

1 view (last 30 days)
Hi !
I'm trying to plot 8 matrixes (C1 to C8) on 1 plot. For each ploted matrix, I want the corresponding legend (C1 to C8). Below is my code and when I run it, MATLAB returns me :
"Function 'subsindex' is not defined for values of class 'cell'.
Error in matlab_common_plot (line 16) legend(Legend)"
numberfile = 8
figure
title('Spannung vs. Traversenweg - - C')
for i=1:numberfile
mat_x=eval(['C',num2str(i)]);
plot(mat_x(:,2),mat_x(:,6),'-')
hold all
end
hold off
xlabel('Traversenweg (mm)')
ylabel('Spannung (MPa)')
Legend = cellstr( num2str((1:numberfile)','C%d') );
legend(Legend)
saveas(gca, 'Spannung vs. Traversenweg - -C.jpg', 'jpg');
close
end

Accepted Answer

Thorsten
Thorsten on 13 Oct 2015
Edited: Thorsten on 13 Oct 2015
I generate some fake data with
for i=1:numberfile, eval(['C' int2str(i) '=rand(10,10);']), end
and the code works fine for me. Can you run this without error?
Maybe you have by accidentally defined legend as a variable. You can check this using
which legend
Should return something like
/Applications/MATLAB_R2012a.app/toolbox/matlab/scribe/legend.m
Note that using variables of that differ only in a single upper-case letter from a Matlab function, like your "Legend" is not a good idea. Use something else, like legendstr.

More Answers (1)

Romain Marchant
Romain Marchant on 13 Oct 2015
Yes, it runs without errors.
which legend
returns "legend is a variable.". How can I change this?
  4 Comments
Thorsten
Thorsten on 13 Oct 2015
Romain, please formally accept my answer and then open a new question to ask a new question.
Romain Marchant
Romain Marchant on 13 Oct 2015
Thank you very much for your answer. I'll open a new question.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!