rjzii
rjzii

Reputation: 14543

How do you convert a table with mixed data types to a single data type?

I have a table in defined in Matlab that is a mixture of strings and integers, i.e.

    A             B         C         D         D
    ----------    ---       ---       ---       ---
    1.0002e+05    '2'       '2'       '1'       '2'   
    1.0002e+05    '2'       '1'       '2'       '2'   
    1.0002e+05    '2'       '2'       '1'       '1'   
    1.0003e+05    '2'       '2'       '2'       '1'   
    1.0003e+05    '2'       '2'       '3'       '1'   

Thanks to some data cleansing I know that the only thing let is integer data. As such, how can I apply str2num to the table so that everything is updated at once?

Upvotes: 1

Views: 531

Answers (2)

sco1
sco1

Reputation: 12214

An alternative approach utilizing dynamic field referencing, which work for tables as well.

Assuming your table T is set up as above:

myvarnames = T.Properties.VariableNames;
for ii = 1:length(myvarnames)
    if iscell(T.(myvarnames{ii}))
        % If your table variable contains strings then we will have a cell
        % array. If it's numeric data it will just be in a numeric array
        T.(myvarnames{ii}) = str2double(T.(myvarnames{ii}));
    end
end

We can also use varfun (e.g. T = varfun(@str2double, T)) but in this case it will error out because not all of the variables are strings. I'm assuming this is the case but the error message is pretty cryptic (MATLAB:table:RowDimensionMismatch). You can catch this with a try/catch block but you run the risk of passing on an actual dimension mismatch, which I don't like. You can also utilize the InputVariables name-value pair if you know explicitly which variables you want to pass.

varfun also annoyingly renames the variables that are passed. Granted, it only takes a one liner to fix it but it's still annoying.

Upvotes: 1

rjzii
rjzii

Reputation: 14543

I suspect there might be a cleaner way of doing this, but assuming that the table is named data,

% Convert the data new table that contains the numeric data
conversion = array2table(cellfun(@str2num, table2cell(data(:, 2:end))));

% Copy the column names over
conversion.Properties.VariableNames = data.Properties.VariableNames(2:end);

% Delete the old columns
data(:, 2:end) = [];

% Concatenate the new table
data = [data conversion];

Upvotes: 0

Related Questions