How can I add multiple 1 x 1 strings to all rows in a table?

1 回表示 (過去 30 日間)
Benjamin Horsley
Benjamin Horsley 2021 年 3 月 5 日
回答済み: Walter Roberson 2021 年 3 月 5 日
Here is a mock table.
% Mock table.
headers = {'A' 'B' 'C' 'D' 'E'};
data = cell(4,5);
T = cell2table(data);
T.Properties.VariableNames = headers;
I am trying to add four new columns at the beginning of T where each column contains the respective workspace variable across all rows.
par = "p00";
type = "Pilot";
trial_no = 1;
vel = "Moderate";
I'm a bit stuck on how to do this, so will appreciate any help.
Thank you.

採用された回答

Walter Roberson
Walter Roberson 2021 年 3 月 5 日
You can use 'Before' and 'after' to add the variables into a particular location in the table.
Note: you will need to supply values for all of the rows at the time you add the variables.
Much of the time it is easier to do something like
T.par = appropriate values
T.type = appropriate values
T.trial_no = appropriate values
T.vel = appropriate values
T = T(:,{'par', 'type', 'trial_no', 'vel', headers{:}});
which is to say, add the variables one by one, and then afterwards re-order the variables to the final order.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMock Dependencies in Tests についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by