How set table column to modified data?

2 visualizaciones (últimos 30 días)
Struggling in MATLAB
Struggling in MATLAB el 11 de Ag. de 2022
Comentada: Struggling in MATLAB el 11 de Ag. de 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?

Respuesta aceptada

Karim
Karim el 11 de Ag. de 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

Más respuestas (0)

Categorías

Más información sobre Numeric Types en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by