How set table column to modified data?

2 次查看(过去 30 天)
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by