How to write a for loop to add to a label
14 views (last 30 days)
Show older comments
I have labels from a tabel that I have extracted from the table but I am trying to add a _x, _y, and _z to each indivdual label. For example if the labels were 'arm' and 'leg' the end result would be 'arm_x, arm_y, and arm_z' . I want to do this using a for loop but I have no idea how to do this
0 Comments
Answers (1)
Kevin Holly
on 6 Nov 2022
Edited: Kevin Holly
on 6 Nov 2022
t = array2table(rand(3,3));
t.Properties.VariableNames = {'arm','leg','foot'}
for ii = 1:length(t.Properties.VariableNames)
t.Properties.VariableNames{ii} = [t.Properties.VariableNames{ii} '_x'];
end
t
Assuming you have the data in a matrix:
header = ["arm","arm","arm","leg","leg","leg","foot","foot","foot"];
data = rand(3,9);
m=[header;data]
t2 = array2table(data);
for ii = 1:3:length(header)
header(ii) = convertCharsToStrings([char(header(ii)) '_x']);
end
for ii = 2:3:length(header)
header(ii) = convertCharsToStrings([char(header(ii)) '_y']);
end
for ii = 3:3:length(header)
header(ii) = convertCharsToStrings([char(header(ii)) '_z']);
end
t2.Properties.VariableNames = header
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!