convert a txt file with complex numbers to a matrix
조회 수: 3 (최근 30일)
이전 댓글 표시
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 "?
댓글 수: 0
채택된 답변
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};
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 String Parsing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!