Matlab: How to read in numbers with a comma as decimal separator?
An answer to this question on Stack Overflow.
Question
I have a whole lot (hundreds of thousands) of rather large (>0.5MB) files, where data are numerical, but with a comma as decimal separator.
It's impractical for me to use an external tool like sed "s/,/./g".
When the separator is a dot, I just use textscan(fid, '%f%f%f'), but I see no option to change the decimal separator.
How can I read such a file in an efficient manner?
Sample line from a file:
5,040000 18,040000 -0,030000
Note: There is a similar question for R, but I use Matlab.
Answer
My solution (assumes commas are only used as decimal place holders and that white space delineates columns):
fid = fopen("FILENAME");
indat = fread(fid, '*char');
fclose(fid);
indat = strrep(indat, ',', '.');
[colA, colB] = strread(indat, '%f %f');
If you should happen to need to remove a single header line, as I did, then this should work:
fid = fopen("FILENAME"); %Open file
indat = fread(fid, '*char'); %Read in the entire file as characters
fclose(fid); %Close file
indat = strrep(indat, ',', '.'); %Replace commas with periods
endheader=strfind(indat,13); %Find first newline
indat=indat(endheader+1:size(indat,2)); %Extract all characters after first new line
[colA, colB] = strread(indat, '%f %f'); %Convert string to numerical data