为matlab中的所有行添加一个相同字符名称的列

Adding a column of the same character name for all the rows in matlab

我在 MATLAB 中有一个 table,我想添加一个包含字符变量的列,该变量在整个列中都相同,但大小长度不同。这是两个随机 tables.

的代码
r1 = rand(100,1);
r1 = array2table(r1);
r1.Properties.VariableNames = {'Random1'};

r2 = rand(54,1);
r2 = array2table(r2);
r2.Properties.VariableNames = {'Random2'};

有没有办法,例如,我可以根据 table 的行数为两者添加一个名为 'Time' 的包含字符 'hours' 的列?

您只需将新数组添加到表中即可:

r1.Time = repmat('hours', [length(r1.Random1), 1]);
r2.Time = repmat('hours', [length(r2.Random2), 1]);