How do I set legend labels depending on number of plotted data?
9 views (last 30 days)
maybe this is the last question?
I have a figure with N plotted functions/graphs. The function LPFigurefun below handles everything with relation to the final figure, so axes limits and names, title, etc. What is missing is that right now, if I plot 4 functions, the legend shows:
How would I implement that each of these labels is replaced by one I want. I thought about setting up something like this:
This would work perfectly as long as the person edits this automatically. My problems now are:
1) This is all wonderful as long as I have 2 graphs and 2 data sets. But how do I get this to adjust according to the number of plots.
So if I have 6 plotted graphs, I need 12 inputs in the teststring that can be grabbed.
2) Similarly, I would need the legend row to adjust automatically to how many entries that string has. So the approach above doesn't work, because it doesn't scale itself depending on the length of teststring*2 to accomodate for all graphs and data sets.
function [LPAxis_vector] = LPFigurefun %LPFigureSettings sets the following: axis, inserts a legend, Title, axis labels, gridsettings etc.
title(inputdlg('What is the title?'))
xlabel(inputdlg('What is the x-label?'))
ylabel(inputdlg('What is the y-label?'))
answer= inputdlg('What are the axis limits?');
LPLocationSetting=listdlg('ListString',LPLocationlist,'PromptString','Select Legend location','Name','Loation-Selection','SelectionMode','Single','ListSize',[160 130]);
LPGrid=questdlg('Grid on or off?','Do you have grit?','On major only','On minor too','Off','On');
case 'On major only'
case 'On minor too'
As always, I am thankful for any advice. This forum has been beyond helpful for me so far. I definetly know at least a lot more than before going here on a daily basis.
Peng Li on 28 Mar 2020
So using your style
legendStr = ; for iL = 1:numberOfObj legendStr = [legendStr; string(inputdlg([‘what is the legend # ’ num2str(iL)])); end
legendStr grows as your loop goes.
If this is what you need based on your description and code. It’s cumbersome for me honestly.