MATLAB Answers

Building the legend according to user input.

3 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

  0 Comments

Sign in to comment.

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

surendra kumar Aralapura mariyappa
@ Jan,
,' where k is the appropriate index'.
This is what I am looking for exactly,
X = {[ 2 4 6] ['Node2' 'Node4' 'Node6']};
If I got to know the indicies of X{1}, before the solver enter the plot function, I can use that indicies to get the suitable legend to that number like X{2}(). For an example if c = 2, then legend is Node2.
Let's consider c = 4, then it's index is X{1}(1,2) this I can write as X{1}(row, col).
So in your solution I can write it as
plot(f(1:200:end)/3600,T1(c,1:200:end),'DisplayName',X{1+1}{col, row)
Or else, kindly tell me or give me an hint, to get the legend to my above quwstion. your collegaue has given me a solution, but it is not working as I intended.
I still remember that you answered to one of the questions similar to my question. But I am not able to get it again.
Hope you can understand now
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!