Adding legend of different names in a plot generated by a for loop

21 Ansichten (letzte 30 Tage)
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!

Akzeptierte Antwort

Benjamin Kraus
Benjamin Kraus am 11 Feb. 2016
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:
  2 Kommentare
Tejaswini Ramesh
Tejaswini Ramesh am 17 Feb. 2016
Bearbeitet: Stephen23 am 17 Feb. 2016
Thank you so much for the help! I incorporated this in my code but it is not working completely for me. I perform the battery tests for two different temperatures-->i at each different temperatures, there are different cells (whose names ans details are stored in folder called cell list) under test, that gives j. therefore i have to plot the different cell names in the legend. Please guide me. Here is the code: 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: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 % Go Through the two differnet temperatures
Index = find(Cal.Temp == Temp(i));
subplot(1, 3, i); % Definition of the subplot
for j=1:size(Index, 2)
%sort dates
d = 0
for d=1:size(M5BAT.CellList(Cal.Index(Index(j))).ResultList,2)
d
DateVec(d) = M5BAT.CellList(Cal.Index(Index(j))).ResultList(d).Sort;
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);
YValue(k) = CrealVec(k) / CrealVec(1) * 100;
end
for p = 1:size(M5BAT.CellList(Cal.Index(Index(j))).ResultList,2);
% plot(XValue , YValue, '-xb', 'Color', Colour(ceil(j/1),:), 'LineWidth', 2, 'MarkerSize', 8);
names{p} = ['Cell Number ' num2str(p)];
end
legend((names),'Location','Southeast');
if d > 0
plot(XValue , YValue, '-xb', 'Color', Colour(ceil(j/1),:), 'LineWidth', 2, 'MarkerSize', 8);
% names{p} = ['Cell Number ' num2str(M5BAT.CellList(Cal.Index(Index(j))))];
hold on;
clear XValue YValue;
% for p=1:size(M5BAT.CellList(Cal.Index(Index(j))).ResultList,2);
% names{p} = ['Cell.number = ', num2str(p)];
% end
% legend((names),'Location','Southeast');
% hold on
legend((names),'Location','Southeast');
end
end
axis([0 150 85 103]);
grid on;
xlabel('Storage Time / Days');
ylabel('Capacity / %');
title(['Ageing at ' num2str(Temp(i)) '°C']);
end
Tejaswini Ramesh
Tejaswini Ramesh am 22 Feb. 2016
It worked :) Thank you very much for your support :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by