필터 지우기
필터 지우기

How can I automatically fill in the legend?

조회 수: 16 (최근 30일)
Thomas Schot
Thomas Schot 2016년 5월 4일
댓글: Sarlota Duskova 2020년 4월 14일
Hi all,
I was wondering how I can automatically fill in my legend of a plot with the names of the files I load regarding excel-files (those contain numerical matrices). The code I'm using is:
material = {xlsread('dp600_2_layers_L50.xlsx',4),xlsread('dp600_3_layers_L50.xlsx',4),xlsread('dp800_3_layers_L50.xlsx',4)};
legend(material);
Clearly, I want to name the first line "dp600_2_layers_L50", the second line "dp600_3_layers_L50", etc. The error which Matlab returns is "Cell array argument must be a cell array of strings.", which seems logical. Is there any way to work around this, or do I have to fill in the legend manually each time I change the input files?
Thank you all in advance!
Thomas

채택된 답변

CS Researcher
CS Researcher 2016년 5월 4일
Let us assume there are N files. You can do this:
legendTitle = cell(1,N);
for i = 1:N
%Your other operations
[~,fileName,~] = fileparts(file) % e.g., file is 'dp600_2_layers_L50.xlsx'
legendTitle{1,i} = fileName;
end
plot() % Plot whatever you need to
legend(legendTitle);
Hope this helps!
  댓글 수: 2
Paolo Edera
Paolo Edera 2019년 5월 5일
Thankyou very much!!
Sarlota Duskova
Sarlota Duskova 2020년 4월 14일
Hello, Can I ask, I have similar problem. My code is look like this:
legendTitle = getappdata(0, 'Text')
for j = 1:nFiles
filename = cell2mat(legendTitle(j))
[~,fileName,~] = fileparts(filename);
legendTitle{1,j} = fileName
end
legend(app.axes1,{legendTitle,'Temperature'},
but it doesnot work because legendTitle is in cell array. It is work when I do this:
legend(app.axes1,legendTitle)
but I am reading multiple csv file and I want to have legends names then which is contain in legendTitle but then I have second curv which is everytime Temperature. And I need show the same count in legend as is choosen files. So how can I put name Temperature into legend which already contains legendTitle which is in cell array? Thank you for your reply.

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

추가 답변 (1개)

Thomas Schot
Thomas Schot 2016년 5월 4일
Works like a charm! Thanks!

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by