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 Center および File ExchangeText Files についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by