Error csvread() numeric field -> *i

조회 수: 3 (최근 30일)
Raphael Werner
Raphael Werner 2021년 7월 2일
답변: Jeremy Hughes 2021년 7월 3일
Hello
I get an error while using csvread() to import multiple .csv files into a matrix
data in csv files:
......141-37*i, 143-37*i, 145-37*i, .........
---------------------------------------------------------------------------------------------
Error using dlmread (line 147)
Mismatch between file and format character vector.
Trouble reading 'Numeric' field from file (row
number 1, field number 3) ==> *i, 143-37*i,
145-37*i, 147-37*i, 149-37*i, 151-37*i, 153-37*i,
155-37*i, 157-37*i, 159-37*i, 161-37*i, 163-37*i,
165-37*i, 167-37*i, 168-37*i, 169-37*i, 170-37*i,
171-37*i, 172-37*i, 173-37*i, 174-37...
Error in csvread (line 48)
m=dlmread(filename, ',', r, c);
Error in extract_heights (line 51)
RAWDATA = csvread("tmp/" + files(idx));
---------------------------------------------------------------------------------------------
Can somebody please help me with this?
  댓글 수: 1
dpb
dpb 2021년 7월 2일
dlmread isn't smart enough to read comlex variables.
I think readmatrix may; give it a go; it would help anybody to attach a part of the input file..

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

답변 (2개)

dpb
dpb 2021년 7월 2일
편집: dpb 2021년 7월 3일
In the far distant past I railed at TMW for not adding the complex variable into the various input routines -- for a scientific "rapid development" package the lack built in handling for them is just absolutely incomprehensible.
readmatrix did fail, too... :(
You've got to do the hard work yourself still...
> type rw.csv % sample input file...
145-37*i, 147-37*i, 149-37*i, 151-37*i, 153-37*i,
155-37*i, 157-37*i, 159-37*i, 161-37*i, 163-37*i,
165-37*i, 167-37*i, 168-37*i, 169-37*i, 170-37*i,
>>
fmt='%f%f*i,'
fid=fopen('rw.csv','r');
data=cell2mat(textscan(fid,fmt));
fid=fclose(fid);
data=reshape(complex(data(:,1),data(:,2)),5,[]).'
returns
>> data
data =
1.0e+02 *
1.4500 - 0.3700i 1.4700 - 0.3700i 1.4900 - 0.3700i 1.5100 - 0.3700i 1.5300 - 0.3700i
1.5500 - 0.3700i 1.5700 - 0.3700i 1.5900 - 0.3700i 1.6100 - 0.3700i 1.6300 - 0.3700i
1.6500 - 0.3700i 1.6700 - 0.3700i 1.6800 - 0.3700i 1.6900 - 0.3700i 1.7000 - 0.3700i
>>
Two caveats -- above needs the trailing delimiter after every value and have to know the shape to return...
ADDENDUM:
Since have to use the file handle for textscan anyways, you can get the number of fields pretty easily and avoid the reshape by
fmt='%f%f*i,'
fid=fopen('rw.csv','r');
l=fgetl(fid); % bring in first record as char vector
n=sum(l==','); % count delimiters in record
frewind(fid) % reset the file pointer to beginning of file
data=cell2mat(textscan(fid,repmat(fmt,1,n)));
fid=fclose(fid);
data=complex(data(:,1:2:end),data(:,2:2:end));

Jeremy Hughes
Jeremy Hughes 2021년 7월 3일
It it weren't for the "*" character this would work. Something like "1-2i" will be read as complex numbers.
If you have control on the input file, and can change how they are written, you could modify it. Otherwise, reading in, modifying and writing out the file again would be the next best thing.
data = fileread(filename);
data(data == '*')=[];
fid = fopen('newfile.csv','w');
fprintf(fid,'%s',data);
fclose(fid);
A = readmatrix('newfile.csv');

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by