corresponding legends for each curves in one plot

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

 채택된 답변

Thorsten
Thorsten 2015년 10월 13일
편집: Thorsten 2015년 10월 13일
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.

추가 답변 (1개)

Romain Marchant
Romain Marchant 2015년 10월 13일
Yes, it runs without errors.
which legend
returns "legend is a variable.". How can I change this?

댓글 수: 4

Thorsten
Thorsten 2015년 10월 13일
편집: Thorsten 2015년 10월 13일
clear legend
And check that you have not used legend as a variable anywhere in your program. But if you have so, rename it.
And please use the comment function instead of posting your comments as a new answer.
Of course, that was a silly question ...
Oops, sorry. First time on that forum.
I've got another question : For now, I can import -=C1.txt to -=C8.txt file one after another into matrixes C1 to C8. (example below with matrix C8) What I want to do is making a for loop to import those 8 datas in 1 run. To do so, I need to change those numbers "/Users/.../-=C 8.txt" and "C 8 = cell2mat(raw);" for each increment (from 1 to 8). I tried sprint(), str2num() and many others without finding any solutions. PS: that code has been generated by the Import Wizard in Matlab.
filename = '/Users/.../-=C8.txt';
delimiter = '\t';
startRow = 3;
formatSpec = '%s%s%s%s%s%s%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'HeaderLines' ,startRow-1, 'ReturnOnError', false);
fclose(fileID);
%turn point into comma ... blablabla
C8 = cell2mat(raw);
clearvars filename delimiter startRow formatSpec fileID dataArray ans raw col numericData rawData row regexstr result numbers invalidThousandsSeparator thousandsRegExp me R;
Thorsten
Thorsten 2015년 10월 13일
Romain, please formally accept my answer and then open a new question to ask a new question.
Thank you very much for your answer. I'll open a new question.

댓글을 달려면 로그인하십시오.

카테고리

태그

질문:

2015년 10월 13일

댓글:

2015년 10월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by