MATLAB: How to speed up code for converting an array of strings to array of numbers

findMATLAB

I have a cell containing a large vector (over 1.5M entries ) of strings. Many entries are common, so that there are about 15k unique entries in this vector. I need to convert the array of strings to an array of numbers, where there is one-to-one correspondence between each string and the corresponding number. Each string contains 32 characters. I am using the following code that is taking over half hour to run. I suspect that the for-loop is the culprit. I would really appreciate it if anyone can suggest a faster way of accomplishing the same, since I have to process many datasets?
Thanks for your help.
data_raw = textscan(fid,'%s %f %f %f %f %s %s %s %s %s','Delimiter',',');
%data_raw = textscan(fid,'%f %f %f %f %f %s %s %s %s %s %s','Delimiter',',');
% following code converts alpha TTIDs to numeric TTIDs
unique_TTIDs_alpha = string(unique(data_raw{1}));
n_points = length(data_raw{1,1});
n_points_unique = length(unique_TTIDs_alpha);
TTIDs_numeric = zeros(n_points,1);
tic
for ii = 1: n_points_unique
idx = find(data_raw{1} == unique_TTIDs_alpha(ii));
TTIDs_numeric(idx,1) = ii;
end

Best Answer

I have no idea what your final goal is.
Note that the unique function has as as many as 3 outputs. The first are the unique elements, the second are the indices of the first occurrence of each unique element, and the third are indices (corresponding to the first 2 outputs) of each element, so that replicated elements all get the same number. See the documentation on the unique function for a full discussion.