Search code examples
matlabmatlab-table

Change string values to number in Matlab table


I am using Matlab2015b. And I would like to read a simple csv file to a table and change its string values to corresponding numeric values.

I have following example data.

Var1, VarClass
1 , attack
2 , normal
1, attack

I would like to change this string values to number. for example attack = 1, normal = -1.

My first try.

T = readtable('example_data.csv', 'Delimiter',',','FileType','text','TreatAsEmpty',{'?'});

rows_attack = strcmp(T(:,2),'attack');
T(rows_attack,2) = 1

rows_normal = strcmp(T(:,2),'normal');
T(rows_normal,2) = -1

I get following error:

Undefined function 'eq' for input arguments of type 'cell'.

What? Which undefined function? What is 'eq'?

Well. After reading some about table, I understood that supposedly higher level matlab does not override '=='. That is 'eq' which means equality. But error message certainly is not informative.

Then my second try.

T = readtable('example_data.csv', 'Delimiter',',','FileType','text','TreatAsEmpty',{'?'});


rows_attack = strcmp(T.VarClass,'attack');
T(rows_attack,2) = 1

This time, I get

Right hand side of an assignment into a table must be another table or a cell array.

Well. Okay. It wants table. I will give it one.

T = readtable('example_data.csv', 'Delimiter',',','FileType','text','TreatAsEmpty',{'?'});

rows_attack = strcmp(T.VarClass,'attack');
rows_attack_size = sum(rows_attack);
data_to_fill = ones(rows_attack_size,1) ;
T(rows_attack,2) = array2table(data_to_fill);

Well. This time error message is.

Conversion to cell from double is not possible.

I thought that this matlab table is similar to R data-frame or python pandas DataFrame. Well, certainly it is not. Can someone guide me about how to solve this problem?


Solution

  • Well. Thanks to @Yvon, I found my mistake and solved my problem. Following code works.

    T = readtable('example_data.csv', 'Delimiter',',','FileType','text','TreatAsEmpty',{'?'});
    
    
     rows_attack = strcmp(T.VarClass,'attack');
     T.VarClass(rows_attack) = {-1};
    
     rows_normal = strcmp(T.VarClass,'normal');
     T.VarClass(rows_normal) = {1};
    
     T.VarClass = cell2mat(T.VarClass);