Nevermind. I got it figured out. I was over complicating things by putting all my lines in an array rather than doing it as a vector. Looking at the actual command lines for the legend command was able to get my line handles in a vector and strings in array and get it working.
Legend from two axes in a loop
2 views (last 30 days)
Show older comments
I have constructed a plot with 2 axes for creating charge/discharge hysteresis. The primary axes contains all discharge curves and secondary has all charge curves. To allow multiple cell comparisons these curves are creating in a loop as shown below.
figure;
axis([0 xmax 2 4.4])
ylabel('Voltage');
xlabel('Capacity mAh/cm2');
ax1 = gca;
ax1_pos = ax1.Position;
ax1.XColor = 'r';
ax1.YColor = 'r';
for file_num = 1:files
dline{file_num} = line(d_cap_area{file_num},d_volt{file_num});
dline{file_num}.LineStyle = ls1;
dline{file_num}.Color = color{file_num};
dline{file_num}.Marker = mark{file_num};
end
ax2 = axes('Position',ax1_pos,'XAxisLocation','top','YAxisLocation','right','Color','none');
ax2.XDir = 'reverse';
linkaxes([ax1 ax2],'y');
for file_num = 1:files
cline{file_num} = line(c_cap_area{file_num},flipud(c_volt{file_num}));
cline{file_num}.LineStyle = ls2;
cline{file_num}.Color = color{file_num};
cline{file_num}.Marker = mark{file_num};
end
ax1.YLim = [2 4.6];
ax1.YTick = [2:0.25:4.6];
ax2.YTick = [2:0.25:4.6];
After a user prompt comes up to name all cells (give them their labels) this is also done in a loop to account for whether you have 2 cells or 100 cells.
%label selection%
for file_num = 1:files
prompt = {['Cell Name ' num2str(file_num)]};
dlg_title = 'Input';
answer = inputdlg(prompt,dlg_title);
cd = 'Discharge';
cc = 'Charge';
d_label{file_num} = [answer{1} cd];
c_label{file_num} = [answer{1} cc];
end
prompt = {'Graph Title'};
dlg_title = 'Input';
answer = inputdlg(prompt,dlg_title);
Now I need to make the legend. In the past, when I was comparing a set number of cells I used the following, but I need the legend to adjust with the number of cells. Can I use a loop to build a legend or create a legend using an array of the line data and label data?
LEGH = legend([dline1 dline2 dline3 cline1 cline2 cline3],d_label,d_label2,d_label3,c_label,c_label2,c_label3,'Location','SouthWest');
Answers (0)
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!