![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/172781/image.png)
Adding legend of different names in a plot generated by a for loop
3 ビュー (過去 30 日間)
古いコメントを表示
Tejaswini Ramesh
2016 年 2 月 11 日
コメント済み: Tejaswini Ramesh
2016 年 2 月 22 日
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/154700/image.png)
I am performing Data Analysis for calenderic tests in various batteries. The various cell names are stored in M5BAT.CellList folder. I plot the graph for two different temperature ranges as shown below. I want to include the legend as the cell name or number chosen for different temperatures for every graph. close all clear all load('M5BAT_analysis_all.mat') plottype = 3;
%General constants
%MAXCYC = 30000;
MAXSOH = 103;
%MINSOH = 95;
MINSOH = 10;
Colour = colormap(jet(8));
Temp = [25 50];
CyclingDepth = [100 50 10 2];
CurrentRate = [0.75 1];
% Constants for different calendric test options
Cal.Num = [1:9];
Cal.Temp = [50 50 25 25 50 50 50 50 50];
Cal.SoC = [10 50 50 50 30 50 50 80 100];
% Set up a List where the direct pointers to all elements are stored
% for calendric Cells
for i=1:size(Cal.Num, 2)
for j=1:size(M5BAT.CellList, 2)
if M5BAT.CellList(j).Number == Cal.Num(i)
Cal.Index(i) = j; % Cal.Index = 1 2 3 4 5 6 7 8 9
end
end
end
for i=1:2 % it loops for different temperatures, 1 for 25 degree and for both 25 and 50 degree Celsius
% Go Through the two differnet temperatures
Index = find(Cal.Temp == Temp(i));
subplot(1, 3, i); % Definition of the subplot (three subplot in this case)
for j=1:size(Index, 2)
%sort dates
d = 0; %initiation
for d=1:size(M5BAT.CellList(Cal.Index(Index(j))).ResultList,2)% gives the number of cells corresponding to
% that particular temperature
DateVec(d) = M5BAT.CellList(Cal.Index(Index(j))).ResultList(d).Sort; %converts date and time to vector
CrealVec(d)= M5BAT.CellList(Cal.Index(Index(j))).ResultList(d).Creal10;
%DateVec_1(d) = M5BAT.CellList(Cal.Index(Index(j))).ResultList(d).Sort;
%CrealVec_1(d)= M5BAT.CellList(Cal.Index(Index(j))).ResultList(d).Creal10;
%DateVec_2(d) = M5BAT.CellList(Cal.Index(Index(j-2))).ResultList(d).Sort;
%CrealVec_2(d)= M5BAT.CellList(Cal.Index(Index(j-2))).ResultList(d).Creal10;
end
%calculate days in between checkups and capacity
for k=1:size(M5BAT.CellList(Cal.Index(Index(j))).ResultList,2)
XValue(k) = DateVec(k) - DateVec(1); %storage days
YValue(k) = CrealVec(k) / CrealVec(1) * 100; %capacity
end
if d > 0
plot(XValue , YValue, '-xb', 'Color', Colour(ceil(j),:), 'LineWidth', 2, 'MarkerSize', 8);
hold on;
% legendInfo {j}= ['Cell.number='{M5BAT.CellList(j).result}];
% legend(legendInfo));
clear XValue YValue;
end
end
axis([0 150 85 103]);
grid on;
xlabel('Storage Time / in Days');
ylabel('Capacity / in %');
% legend(['Cell.number = ' num2str(M5BAT.CellList(Index(j)))]);
title(['Ageing at ' num2str(Temp(i)) '°C']);
legendInfo{j}=(['Cell.name = ' num2str(j)]);
legend(legendInfo);
% for p=1:size(M5BAT.CellList(Cal.Index(Index(j))).ResultList,2);
% legendInfo{p}= ['Cell.number = ', {p}];
% legend(legendInfo)
%end
end
I tried using legendInfo but it is not working. I will be grateful for any help regarding this. I have attached the plot for this code. I want to include the name of different cells considered for that particular temperature. Please help me about how i could do that. Thanks in advance!
0 件のコメント
採用された回答
Benjamin Kraus
2016 年 2 月 11 日
It isn't clear what you mean by "various cell names are stored in M5BAT.CellList folder", but it looks like you are trying to create a set of names for the various lines in your plot.
Here is (some of) the code you tried:
for p=1:size(M5BAT.CellList(Cal.Index(Index(j))).ResultList,2);
legendInfo{p}= ['Cell.number = ', {p}];
legend(legendInfo)
end
I see at least two issues with that code:
1. You are trying to concatenate the number ('p') to a string. You can do that using 'num2str' or 'sprintf':
legendInfo{p} = ['Cell.number = ', num2str(p)];
or
legendInfo{p} = sprintf('Cell.number = %d', p);
2. You want to make only a single call to the legend command that includes all the names:
for p=1:size(M5BAT.CellList(Cal.Index(Index(j))).ResultList,2);
names{p} = ['Cell.number = ', num2str(p)];
end
legend(names);
For example, here is sample code using some simple data:
hold on
for p = 1:5;
names{p} = ['Line Number ' num2str(p)];
plot(1:10,(1:10)+p)
end
legend(names)
An alternative approach that may work for you is to use the 'DisplayName' property when calling 'plot':
hold on
for p = 1:5;
name = ['Line Number ' num2str(p)];
plot(1:10,(1:10)+p, 'DisplayName', name)
end
legend show
Both sets of code will produce the same image:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/172781/image.png)
2 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Legend についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!