Why doesn´t conversion from .ASC / .csv / .txt to .xls work?
5 views (last 30 days)
Show older comments
Hello,
I try to convert multible ".ASC" files to ".xls" files in order to process the files in another script. I have read through many Q&A on this site, but none of them solved my problem (csvread(), textscan(), ...). They solution that almost worked and which I also found here was:
file = '(1).txt';
delim = ' '; % Your delimiter
data = dlmread(file,delim,38,0);
filename_out = '(1).xls';
xlswrite(filename_out,data)
Unfortunatelly I receive this error message, no matter if the original file is ".ASC" / ".csv" / ".txt":
Error using dlmread (line 147)
Mismatch between file and format string.
Trouble reading 'Numeric' field from file (row number 1, field number 2) ==> ,37500 -6,58426 -1,75743
-0,02922 0,33484 0,03196 0,40585 29,64215 27,91018 -1,42901
\n
The original file is attached. Strangely the code reads the complete first line, but cuts the values in front of the comma of the first field (,37500 should be 89,37500). I don´t understand the problem here. Since I skip all the string/text in the first rows, it should be possible to read the data properly.
I am looking forward for your help, thank you in advance.
1 Comment
Stephen23
on 29 Jan 2019
Your data file uses a decimal comma, which MATLAB does not parse. You will need to convert the commas to decimal points. Search this forum for "decimal comma" to find various ways to approach this conversion.
Answers (1)
Jan
on 29 Jan 2019
Edited: Jan
on 29 Jan 2019
You are struggeling with the commas, which are used as decimal separators. Replace them by dots at first.
Data = fileread(FileName);
Data = strrep(Data, ',', '.');
FID = fopen(NewFileName, 'w');
fwrite(FID, Data, 'char');
fclose(FID);
or
file = memmapfile( filespec, 'writable', true );
file.Data((file.Data == uint8(',')).') = uint8('.');
Alternatively you can import the file as string, convert the comma in the memory and use textscan to import these data.
0 Comments
See Also
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!