MATLAB: Building the legend according to user input.

legend according to user inputMATLAB

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

Best Answer

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.