How do I set legend labels depending on number of plotted data?

17 views (last 30 days)
Hello,
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:
fitted curve
fitted curve
data1
data2.
How would I implement that each of these labels is replaced by one I want. I thought about setting up something like this:
teststring=["name1","name2","name3","name4"]
legend({teststring{1,1},teststring{1,2},teststring{1,3},teststring{1,4}},'Location','bestoutside')
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?');
LPAxis_vector=str2num(answer{1});
axis(LPAxis_vector);
LPLocationlist={'northeast','north','east','southeast','south','southwest','west','northwest','northoutside','northeastoutside','eastoutside','southeastoutside','southoutside','southwestoutside','westoutside','northwestoutside','best','bestoutside'};
LPLocationSetting=listdlg('ListString',LPLocationlist,'PromptString','Select Legend location','Name','Loation-Selection','SelectionMode','Single','ListSize',[160 130]);
if LPLocationSetting==1
legend('Location','northeast')
elseif LPLocationSetting==2
legend('Location','north')
elseif LPLocationSetting==3
legend('Location','east')
elseif LPLocationSetting==4
legend('Location','southeast')
elseif LPLocationSetting==5
legend('Location','south')
elseif LPLocationSetting==6
legend('Location','southwest')
elseif LPLocationSetting==7
legend('Location','west')
elseif LPLocationSetting==8
legend('Location','northwest')
elseif LPLocationSetting==9
legend('Location','northoutside')
elseif LPLocationSetting==10
legend('Location','northeastoutside')
elseif LPLocationSetting==11
legend('Location','eastoutside')
elseif LPLocationSetting==12
legend('Location','southeastoutside')
elseif LPLocationSetting==13
legend('Location','southoutside')
elseif LPLocationSetting==14
legend('Location','southwestoutside')
elseif LPLocationSetting==15
legend('Location','westoutside')
else
legend('Location','northwestoutside')
end
LPGrid=questdlg('Grid on or off?','Do you have grit?','On major only','On minor too','Off','On');
switch LPGrid
case 'On major only'
grid off
grid on
case 'On minor too'
grid off
grid on
grid minor
case 'Off'
grid off
end
end
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.
  3 Comments
darova
darova on 27 Mar 2020
So the question is: how to input N number of string and store them

Sign in to comment.

Accepted Answer

Peng Li
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.

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by