Why doesn´t conversion from .ASC / .csv / .txt to .xls work?

조회 수: 5 (최근 30일)
Matthias Pittrich
Matthias Pittrich 2019년 1월 29일
댓글: Stephen23 2019년 1월 29일
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
Stephen23
Stephen23 2019년 1월 29일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
Jan 2019년 1월 29일
편집: Jan 2019년 1월 29일
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.

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by