Building the legend according to user input.

2 views (last 30 days)
Hey all,
Hope everyone doing good.
I have a small question here.
I need to get the legend according to input.
Below is my code;
n = 50;
[f,T1, X] = Samplemodel_Instat('Measuring_Data.mat');
%X = {[ 2 4 6] ['Node2' 'Node4' 'Node6']};
for plotnumber = 1:n
c = find(X{1,1} == plotnumber,1);
if ~isempty(c)
c = plotnumber;
if n <= 3
plot(f(1:150:end)/3600,T1(c,1:150:end),'Color',[0.831372559070587 0.815686285495758 0.7843137383461]);
% legend(X{1+1}(col,row));
%plot(f(1:200:end)/3600,T1(c,1:200:end),'DisplayName',X{1+1}(col,row));
elseif n > 3
if c >= 1 && c <= ceil(n./4)
plot(f(1:200:end)/3600,T1(c,1:200:end),'Color',[0.25 0.25 0.25],'LineWidth',4,'LineStyle','-.','MarkerFaceColor',[1 1 1],'Marker','none');
% legend(X{1+1}(col,row));
%plot(f(1:200:end)/3600,T1(c,1:200:end),'DisplayName',X{1+1}(col,row));
elseif c > (ceil(n./4)) && c <= ceil(n./2)
plot(f(1:200:end)/3600,T1(c,1:200:end),'Color',[0.952941179275513 0.87058824300766 0.733333349227905],'LineWidth',2,'MarkerFaceColor',[1 1 1],'Marker','s');
% legend(X{1+1}(col,row));
%plot(f(1:200:end)/3600,T1(c,1:200:end),'DisplayName',X{1+1}(col,row));
elseif c > (ceil(n./2)) && c <= ceil(3*n./4)
plot(f(1:200:end)/3600,T1(c,1:200:end),'Color',[0.31 0.31 0.31],'LineWidth',2,'LineStyle','-','Marker','d','MarkerFaceColor',[1 1 1]);
% legend(X{1+1}(col,row));
%plot(f(1:200:end)/3600,T1(c,1:200:end),'DisplayName',X{1+1}(col,row));
elseif c > (ceil(3*n./4)) && c <= n
plot(f(1:150:end)/3600,T1(c,1:150:end),'Color',[0.831372559070587 0.815686285495758 0.7843137383461]);
% legend(X{1+1}(col,row));
%plot(f(1:200:end)/3600,T1(c,1:200:end),'DisplayName',X{1+1}(col,row));
end
end
end
end
Here I need to get the legend everytime for the respective c.
For an example if c = 2, legend should be Oil. You can see how c become 2 in X above.
I hope that it is eary to understand.
Any suggestions and answered are most welcomed.
Thanks in advance

Accepted Answer

Jon
Jon on 1 Jul 2019
It is a little hard to understand all of the details of what you are doing in your code, and what exactly you want to produce for plots. If I am understanding correctly, I think you should be able to do what you want by setting the DisplayName property when you plot the line.So put your possible legend names in a cell array e.g. names = {'Oil','Air','Material'}, and then index appropriately when you plot the line, to set the DisplayNamso something like,
plot(f(1:200:end)/3600,T1(c,1:200:end),'DisplayName',names{k})
, where k is the appropriate index.
  3 Comments
Jon
Jon on 3 Jul 2019
I sorry but I am still having difficulty understanding your application. If I am understanding correctly however I would suggest the following.
First use two separate variables in place of your X, this will make the indexing much simpler and clearer. So for example define
crvIdx = [2 4 6]; % curve index
crvName = {'Node2','Node4','Node6'} % or maybe {'oil','gas','air'} whatever you want the labels to be
then you can use the find function to match the curve index to the plot number
c = find(crvIdx==plotnumber)
and when it comes time to set the legend name you would do something like
plot(f(1:200:end)/3600,T1(c,1:200:end),'DisplayName',crvName{c}
by the way, your intitial code fragment, which I list below, does not make sense to me
n = 50;
[f,T1, X] = Samplemodel_Instat('Measuring_Data.mat');
%X = {[ 2 4 6] ['Node2' 'Node4' 'Node6']};
for plotnumber = 1:n
c = find(X{1,1} == plotnumber,1);
if ~isempty(c)
c = plotnumber;
if n <= 3
plot(f(1:150:end)/3600,T1(c,1:150:end),'Color',[0.831372559070587 0.815686285495758 0.7843137383461]);
First you assign n = 50, so it is a constant value, and then later you check if n<=3, since n will always be 50, this condition would never be true, so I assume this isn't what you intended.
Second you first assign a value to c using the find function, c= find{X{1,..., and then you say if if ~isempty(c), c= plotnumber. So if it finds a value of c, c will not be empty, and then you immediately reassign it to equal the plot number. In this case why bother finding out where c matches, if whenever it finds something you immediately overwrite it. I guess this isn't what you had intended either
surendra kumar Aralapura mariyappa
@Jan
Thank you for your kindness. Actually I was bit confused. Because, I was stressed out myself while doing this. Then I got to know my mistakes. Now It is working properly. All your collegaues helped me out a lot.
Anyways thank you once again.

Sign in to comment.

More Answers (0)

Products


Release

R2014a

Community Treasure Hunt

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

Start Hunting!