How to solve this errror about reading in complex numbers?

3 次查看(过去 30 天)
filename='plot.csv';
fid=fopen(filename,'r');
data=textscan(fid,'%f %f %f %f %f','Delimiter','Whitespace','HeaderLines',5);
fclose(fid);
data1=cell2mat(data);
real=data1(:,4);
img=data1(:,5);
complex=real+1i*img
after reading the data from .csv format, I want only 4th and 5th column only ... only the last 2 columns.
Then get into a complex number = real+1i*img with format(a+bi), but I am getting an error.

回答(2 个)

dpb
dpb 2022-10-9
Previously answered what appears to be virtually identical Q?
The short answer is "read the whole file and save just what need in memory".
  2 个评论
riki singh
riki singh 2022-10-9
i m getting an error in
data1=cell2mat(data) where after converting to matrix
i am nt able to extract last 2 columns
dpb
dpb 2022-10-9
编辑:dpb 2022-10-9
Well, works just fine for me...
filename=websave('plot.csv','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1150415/plot.csv');
fid=fopen(filename,'r');
data=cell2mat(textscan(fid,'','Delimiter',',','HeaderLines',1));
fclose(fid);
Y=complex(data(:,4),data(:,5))
Y =
-4.2000 - 5.2000i -3.2000 + 6.2000i -5.2000 + 3.2000i -3.2000 - 5.2000i -6.2000 - 6.3000i -2.3000 - 9.2000i 2.3000 - 6.2000i 2.3000 - 3.2000i 6.2000 - 6.3000i 3.2000 - 7.8000i
Don't alias builtin complex function; "there be dragons!"

请先登录,再进行评论。


Image Analyst
Image Analyst 2022-10-9
filename='plot.csv';
data= readtable(filename)
data = 10×5 table
a1 a2 b1 b2 C1 __ __ __ ____ ____ 1 0 1 -4.2 -5.2 2 0 2 -3.2 6.2 3 0 3 -5.2 3.2 4 0 4 -3.2 -5.2 5 0 5 -6.2 -6.3 6 0 1 -2.3 -9.2 7 0 2 2.3 -6.2 8 0 3 2.3 -3.2 9 0 4 6.2 -6.3 10 0 5 3.2 -7.8
% Get real component from column 4.
realValues = data{:,4}
realValues = 10×1
-4.2000 -3.2000 -5.2000 -3.2000 -6.2000 -2.3000 2.3000 2.3000 6.2000 3.2000
% Get imaginary component from column 5.
imaginaryValues = data{:,5}
imaginaryValues = 10×1
-5.2000 6.2000 3.2000 -5.2000 -6.3000 -9.2000 -6.2000 -3.2000 -6.3000 -7.8000
complexValues = realValues + 1i * imaginaryValues
complexValues =
-4.2000 - 5.2000i -3.2000 + 6.2000i -5.2000 + 3.2000i -3.2000 - 5.2000i -6.2000 - 6.3000i -2.3000 - 9.2000i 2.3000 - 6.2000i 2.3000 - 3.2000i 6.2000 - 6.3000i 3.2000 - 7.8000i

类别

Help CenterFile Exchange 中查找有关 Large Files and Big Data 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by