MATLAB: Convert an string stored in a cell to a number

cell conversionstring conversionstring to number conversion

I have imported a log (a .log file) into Matlab and used textscan to convert the .log file into a cell.
One of the entries in the log is is an "error state" that consists of symbols (e.g. "N" – no error, "E" – battery less than 10%, "M" – mechanical problem, etc.).
I have successfully imported this data into a cell of strings. However, what I would like to do is to convert each of these symbols for error state to a corresponding number, so I can plot the error state (with respect to time) and thus be able to "see" the error state change over time. For example, the "N" error would correspond to 1, the "E" state would correspond to 2, the "M" state would correspond to 3, etc.
The cell, "errorState" is the following format: errorState: {118×1 cell}
Here is what I am have attempted: idx_n = find(strcmp(errorState,'N')); errorState = char(errorState(idx_n)); errorState(idx_n) = '1'; errorState(idx_n) = str2num(errorState(idx_n));
I can successfully convert it to the "char" type with '1' to the following: errorState: [118×1 char].
But, then the result of trying to convert to a numerical array (str2num) results to an empty array with a type [118×1 char].
Is there any way to convert the cell array of 'N', 'E', 'M', etc. to corresponding numbers, so I can eventually plot those numbers?
Thanks for your help!

Best Answer

You could use ismember:
>> key = {'N','E','M'};
>> data = {'E','M','N','M','E','N'};
>> [~,loc] = ismember(data,key)
loc =
2 3 1 3 2 1