saastn
saastn

Reputation: 6015

How to convert table columns to desired types all at once?

I have this csv file that is read to a table variable using readtable. So, all columns are of type cell containing string values. The actual data is of types double, int32, datetime, logical, and string. I'm converting them column by column. I want to convert specific columns that are of same type, or maybe convert all columns at once. Is that possible?

Here's a sample of what I'm doing now, but my table has 63 columns:

clear variables;
%% simulate readtable
C = {...
'34145823','2015-12-08 00:15:55','3.2','1';
'34157170','2015-12-08 13:29:30','0.5','0';
'34157958','2015-12-08 14:07:00','-1.7','1'};
T = table(C(:, 1), C(:, 2), C(:, 3), C(:, 4), ...
    'variablenames', {'int32Col', 'datetimeCol', 'doubleCol', 'logicalCol'});
varfun(@class, T, 'OutputFormat', 'cell')
%% type conversion
T.int32Col = int32(str2double(T.int32Col));
T.datetimeCol = datetime(T.datetimeCol);
T.doubleCol = str2double(T.doubleCol);
T.logicalCol = cell2mat(T.logicalCol) == '1';
varfun(@class, T, 'OutputFormat', 'cell')
T

P.S.: all values in these csv files are enclosed between "s, and that's why they are read as strings.

Upvotes: 2

Views: 1627

Answers (1)

David
David

Reputation: 8459

I think you'll have to at least specify the datatype of each column, but once you've done that a switch statement will let you loop through the columns without having to go through one by one.

I introduced varNames as the name of each column---I presume you can get this from your csv file. I also introduced varTypes which is the datatype of each column, I think you'll have to make this by hand, maybe you can automate it depending on your actual csv files.

clear variables;
% simulate readtable
C = {...
    '34145823','2015-12-08 00:15:55','3.2','1';
    '34157170','2015-12-08 13:29:30','0.5','0';
    '34157958','2015-12-08 14:07:00','-1.7','1'};
varNames = {'int32Col', 'datetimeCol', 'doubleCol', 'logicalCol'};
T = table(C(:, 1), C(:, 2), C(:, 3), C(:, 4), ...
    'variablenames', varNames);

varTypes = {'int32' 'date' 'double' 'logical'};
for i = 1:size(T,2)
    switch varTypes{i}
        case 'int32'
            T.(varNames{i}) = int32(str2double(T{:,i}));
        case 'date'
            T.(varNames{i}) = datetime(T{:,i});
        case 'double'
            T.(varNames{i}) = str2double(T{:,i});
        case 'logical'
            T.(varNames{i}) = logical(cell2mat(T{:,i}));
    end
end

Upvotes: 3

Related Questions