필터 지우기
필터 지우기

convert a txt file with complex numbers to a matrix

조회 수: 3 (최근 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 CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by