How set table column to modified data?

2 ビュー (過去 30 日間)
Struggling in MATLAB
Struggling in MATLAB 2022 年 8 月 11 日
I am performing same set of operations for all the columns in a table. How do I use a for loop over all the columns for this? I want to covert the string entries to double and set the transformed data as new column entries of the existing table.
Here is my sample file attached. The set of operation is as follows.
for col = 1:7
loadfile = load('sample.mat');
rawTableData = loadfile.ans;
thisColumn = rawTableData(:, col);
% remove unit name, decimal places, just keep the value
data = thisColumn.Variables;
strData = string(data);
expression = '(-?\d+(\.\d*)?)|(-?\.\d+)';
regexData = regexp(strData, expression, 'match', 'once');
%% set thisColumn to be regexData
end
How do I do it?

採用された回答

Karim
Karim 2022 年 8 月 11 日
See below, you can first extract the column names from the table. And use these as index during the loop.
loadfile = load(websave('myFile', "https://www.mathworks.com/matlabcentral/answers/uploaded_files/1094055/sample.mat"));
% get the columns of the table
rawTableData = loadfile.ans;
MyCol = rawTableData.Properties.VariableNames;
for col = 1:numel(MyCol)
% remove unit name, decimal places, just keep the value
strData = string(rawTableData.(MyCol{col}));
expression = '(-?\d+(\.\d*)?)|(-?\.\d+)';
regexData = regexp(strData, expression, 'match', 'once');
%% set thisColumn to be regexData
rawTableData.(MyCol{col}) = str2double(regexData);
end
rawTableData
rawTableData = 100×7 table
intensityofcost1 intensityofcost2 intensityofcost3 rewardconcentration1 rewardconcentration2 rewardconcentration3 rewardconcentration4 ________________ ________________ ________________ ____________________ ____________________ ____________________ ____________________ 15 40 240 9 5 2 0.5 15 40 240 9 5 2 0.5 15 40 240 9 5 2 0.5 15 40 240 9 5 2 0.5 15 40 240 9 5 2 0.5 15 40 240 9 5 2 0.5 15 40 240 9 5 2 0.5 15 40 240 9 5 2 0.5 15 40 240 9 5 2 0.5 15 40 240 9 5 2 0.5 15 40 240 9 5 2 0.5 15 40 240 9 5 2 0.5 15 40 240 9 5 2 0.5 15 40 240 9 5 2 0.5 15 40 240 9 5 2 0.5 15 40 240 9 5 2 0.5
  1 件のコメント
Struggling in MATLAB
Struggling in MATLAB 2022 年 8 月 11 日
Thank you very much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by