MATLAB: Is there a faster way than str2double() to convert from a string array into a matrix containing doubles

.txt filesstr2double

Hi, i am working with large .txt files, that I imported as a string array. A big part of this .txt file contains numeric values, that I want to convert to doubles. Since the array is sufficiently large (500.000 x 25), it takes MATLAB very long to convert these strings into doubles using str2double(). Is there a faster way to convert a String array into a numeric matrix?

Best Answer

Importing the strings at first is an indirection. The structure of the file looks easy, so what about using fscanf?
fid = fopen(FileName, 'r');
line1 = fgetl(fid);
line2 = fgetl(fid);
fgetl(fid);
Head = cell(1e6, 1);
Data = cell(1e6, 1); % Pre-allocate
iData = 0;
while ~feof(fid)
iData = iData + 1;
Head = fscanf(fid, '%s'); % Or: strrep(fgetl(fid), ';', '')
Data{iData} = fscanf(fid, '%g;%g;%g;%g', [4, 25]);
end
Head = Head(1:iData);
Data = Data(1:iData);
fclose(fid);
Note that text files are useful, if they are edited or read by a human. Storing 500.000 x 25 numbers in text mode is a really weak design. Storing them in binary format would make the processing much more efficient.