Search code examples
matlabmatlab-table

Separate integer and other numeric columns in MATLAB table


I have a table (csv) stored using datastore function and I'd like to separate the integer columns (categorical) in a table and the float columns (numerical) in another table. I tried the following code

int_col = all(round(Data) == Data,1);
cat_data = Data(:,int_cols);
num_data = Data(:,~int_cols);

but I get the following error

Undefined function round for input of type table


Solution

  • You will want to first convert the table to an array (using table2array) before performing your check for integer values.

    t = table(rand(5,1), randi(5,5,1), 'VariableNames', {'floats', 'ints'});
    
    %// Look for integer columns
    isInt = ~any(mod(table2array(t), 1));
    
    %// Grab the columns that are integers
    integer_table = t(:,isInt);
    
    %// Grab the non-integer columns
    non_integer_table = t(:,~isInt);