How set table column to modified data?

2 Ansichten (letzte 30 Tage)
Struggling in MATLAB
Struggling in MATLAB am 11 Aug. 2022
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?

Akzeptierte Antwort

Karim
Karim am 11 Aug. 2022
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

Weitere Antworten (0)

Kategorien

Mehr zu Numeric Types finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by