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

4 次查看(过去 30 天)
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 个评论
Claudius Simon Appel
No. Let's say the figure contains 8 different objects, so the legend is also 8 entries long (one per set). If that is the case, I need a way to create a vector (or I think I would need a cell, but I am not sure right now) of length 8, store 8 unique character strings (one per slot). Then, the first entry of the legend has to represent the character string in vector(1,1), the second entry should be from vector (1;2) and so on till vectors (1,8) for the last entry of the legend.
The problem is, that if I plot a different number of data sets, lets say twelve, I need this vector to automatically grow to length twelve. It then would have to ask for the twelve unique character strings, and store them. In addition, the legend function would have to assign each character string to one entry on the legend.
How many entries the legend has would have to be set by the number of plotted objects. 24 objects, ask for a vector of length 24, give each slot a unique string, and input these strings into the legend in sequential order. Only two pbjects? Only ask me for two strings, and only create a legend with two entries, and put said strings in. And so on.
darova
darova 2020-3-27
So the question is: how to input N number of string and store them

请先登录,再进行评论。

采纳的回答

Peng Li
Peng Li 2020-3-28
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by