MATLAB: How to find and replace text in an imported table

findreplacetable

I've imported data from an Excel file and have it as a table (DemographicsData). I would now like to find all cells that contain 'Male' and replace them with '1' and 'Female' with '2'. How do I run this command? I'm new with MATLAB and would appreciate any help.

Best Answer

Presumably there's a column in your table for sex|gender and the Male|Female categories are listed in that column. You could use findgroups() to automatically replace M|F with group numbers.
% assuming the table is named T and the
% column is named "sex"
T.sex = findgroups(T.sex);
Another option is to assign those values directly,
T.sex(strcmpi(T.sex,'Male')) = {1};
T.sex(strcmpi(T.sex,'Female')) = {2};
Related Question