How to write a for loop to add to a label
14 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Kevin Holly
2022-11-6
编辑:Kevin Holly
2022-11-6
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 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!