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

回答(1 个)

Kevin Holly
Kevin Holly 2022-11-6
编辑:Kevin Holly 2022-11-6
t = array2table(rand(3,3));
t.Properties.VariableNames = {'arm','leg','foot'}
t = 3×3 table
arm leg foot _______ ________ _______ 0.10922 0.051723 0.94472 0.5496 0.17917 0.75859 0.74596 0.97866 0.98607
for ii = 1:length(t.Properties.VariableNames)
t.Properties.VariableNames{ii} = [t.Properties.VariableNames{ii} '_x'];
end
t
t = 3×3 table
arm_x leg_x foot_x _______ ________ _______ 0.10922 0.051723 0.94472 0.5496 0.17917 0.75859 0.74596 0.97866 0.98607
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]
m = 4×9 string array
"arm" "arm" "arm" "leg" "leg" "leg" "foot" "foot" "foot" "0.28732" "0.35497" "0.77483" "0.906" "0.97643" "0.41476" "0.80085" "0.13739" "0.45569" "0.76207" "0.59204" "0.90457" "0.3132" "0.34569" "0.45569" "0.73151" "0.91559" "0.93208" "0.82099" "0.39515" "0.98561" "0.23393" "0.74316" "0.8982" "0.40011" "0.33814" "0.94749"
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
t2 = 3×9 table
arm_x arm_y arm_z leg_x leg_y leg_z foot_x foot_y foot_z _______ _______ _______ _______ _______ _______ _______ _______ _______ 0.28732 0.35497 0.77483 0.906 0.97643 0.41476 0.80085 0.13739 0.45569 0.76207 0.59204 0.90457 0.3132 0.34569 0.45569 0.73151 0.91559 0.93208 0.82099 0.39515 0.98561 0.23393 0.74316 0.8982 0.40011 0.33814 0.94749

标签

Community Treasure Hunt

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

Start Hunting!

Translated by