フィルターのクリア

convert a txt file with complex numbers to a matrix

2 ビュー (過去 30 日間)
Jeniffer Viegas
Jeniffer Viegas 2018 年 7 月 3 日
コメント済み: Jeniffer Viegas 2018 年 7 月 3 日
Hello, I would like to read this file into Matlab, but give an error.
the file is something like that:
0,0,0,1,8250,0.2,1,"96117.399999999994+12805.799999999999i" 0.49,0,0,3,8250,0.2,1,"99047.899999999994+13444.200000000001i"
and the error are: Unknown text on line number 1 of ASCII file Input_WY_data.dat "96117.399999999994+12805.799999999999i"
or Error using dlmread (line 147) Mismatch between file and format character vector. Trouble reading 'Numeric' field from file (row number 1, field number 8) ==> "96117.399999999994+12805.799999999999i"\n
Someone knows how can I read without the "?

採用された回答

Stephen23
Stephen23 2018 年 7 月 3 日
編集済み: Stephen23 2018 年 7 月 3 日
Why did someone put double quotes around perfectly good complex numbers?
Here are three ways to approach the problem:
1. use dlmread and specify the delimiter:
M = dlmread('temp2.csv',',"');
2. remove double quotes using MATLAB:
% Read text, get rid of double quotes, save text:
str = fileread('temp1.csv');
str = strrep(str,'"','');
[fid,msg] = fopen('temp2.csv','w');
assert(fid>=3,msg)
fprintf(fid,'%s',str);
fclose(fid);
% Read numeric data (including complex values)
M = csvread('temp2.csv');
2. double quotes as delimiters:
opt = {'Delimiter',',"','MultipleDelimsAsOne',true,'CollectOutput',true};
fmt = repmat('%f',1,8);
[fid,msg] = fopen('temp1.csv','rt');
assert(fid>=3,msg)
C = textscan(fid,fmt,opt{:});
fclose(fid);
M = C{1};
  1 件のコメント
Jeniffer Viegas
Jeniffer Viegas 2018 年 7 月 3 日
Well, I just ask the same question, why put "?? I'm working on some data from another person, and seem they like to put ". Anyway, thank you for your answer, I used the 3rd option, double quotes as delimiters. =)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by