Include legends based on column names in for loop

Hi,
I have a loop which processes three tables. Each table has ten columns (in addition, each column includes column names). When I plot the three figures in the loop, I want to include legends which state the column name of each variable within the plot. I know how to make this manually by typing the legends in the Matlab code. However, since the column names are changing depending on table I want to perform this automatically in the script. My though is to use "Display" but I do not know how to apply this kind of approach. In terms of code procedure, I thought something similar to this (not working unfortunately):
vnames=Table_.Properties.VariableNames;
for v = 1:numTables
%Processing of the code
plot(Table_.Col1,Table_{:,3},'DisplayName',vnames(v))
legend show
end
Grateful for any advice.

댓글 수: 1

Thank you! I made some changes to the code and now it works for several tables.
All the best

댓글을 달려면 로그인하십시오.

 채택된 답변

KSSV
KSSV 2022년 1월 12일
id = (1:10)' ;
x = rand(10,1) ;
y = rand(10,1) ;
z = rand(10,1) ;
T = table(id,x,y,z)
T = 10×4 table
id x y z __ ________ ________ ________ 1 0.60378 0.39943 0.005615 2 0.039465 0.2604 0.33861 3 0.24765 0.47166 0.71508 4 0.33116 0.064591 0.014081 5 0.87429 0.19372 0.8671 6 0.80526 0.74176 0.52277 7 0.61993 0.51138 0.34342 8 0.37336 0.11143 0.9837 9 0.73479 0.22456 0.58077 10 0.14989 0.96124 0.2604
figure
hold on
for i = 2:4
plot(T.(1),T.(i),'DisplayName',T.Properties.VariableNames{i})
end
legend

추가 답변 (1개)

Mathieu NOE
Mathieu NOE 2022년 1월 12일
hello
see my little demo below to use the table varibales names as legend
the csv data file is attached
clearvars
clc
%get the data from table
T = readtable('pH_Data.csv'); %Reads Table or CSV File
vnames=T.Properties.VariableNames; % {'DateAndTimeRun1'} {'Time_s_Run1'} {'Voltage_mV_Run1'} {'pHRun1'}
time = T.Time_s_Run1; % time (s)
data = table2array(T(:,3:end)); % data
plot(time,data);
lgstr = vnames(3:end); % use vnames to make legend strings
% legend strings convert underscore to blanks for better legend readability
for ck = 1:numel(lgstr)
tmp = char(lgstr(ck));
ind = strfind(tmp,'_');
tmp(ind) = ' ';
lgstr{ck} = tmp; % update
end
legend(lgstr);

댓글 수: 7

Thank u! I went by the code above since it was was a bit shorter. I tried your code also and it works perfectly also.
All the best!
Hi again, Mathieu.
I'm working with another example in which I want to implement my code. However, it is a bit more complex than your example. I have a table with 12 columns. Each column has a unique id namn consisting of both numbers and characters. II have a loop where I'm plotting this data where three of the columns are plotted in the same plot which gives 4 plots. I want to include legends based on the variable names (uniques id:s). I have problems creating this. I get the first three variables names as legends in each loop. Do you have any idea to solve this?
Thanks
hello again
can you share some data and code ? I'll have a look if I get it to work !
Hi, see attached file for the table I am working with. The code is as follows:
T = readtable('m.xlsx');
matchVals = {'1234', '5678','9101','1123'};
n_match = numel(matchVals);
S = struct('name',matchVals,'data',cell(1,n_match));
for k = 1:n_match
idx = contains(T.Properties.VariableNames,matchVals{k});
figure
plot(1:1:10000,T{:,idx})
fig_name = strcat('Box_',num2str(matchVals{k}));
title(fig_name,'Interpreter','none')
end
To clarify it is the variable names in the table I want as legends.
Thanks again Mathieu
hello
try this :
T = readtable('m.xlsx');
matchVals = {'1234', '5678','9101','1123'};
n_match = numel(matchVals);
% S = struct('name',matchVals,'data',cell(1,n_match)); % what is this for ?
%
vnames=T.Properties.VariableNames; % added this line
for k = 1:n_match
idx = contains(T.Properties.VariableNames,matchVals{k});
figure
plot(1:1:10000,T{:,idx})
fig_name = strcat('Box_',num2str(matchVals{k}));
title(fig_name,'Interpreter','none')
legend(vnames(idx)); % added this line
end
it works perfectly, thank you so much!
I had forgotten to remove some of the code I was working on, hence the comment in the code from my side.
Thank you!
My pleasure !

댓글을 달려면 로그인하십시오.

카테고리

제품

릴리스

R2020b

질문:

2022년 1월 12일

댓글:

2022년 5월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by