How set table column to modified data?

조회 수: 2 (최근 30일)
Struggling in MATLAB
Struggling in MATLAB 2022년 8월 11일
댓글: 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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by