How to solve this errror about reading in complex numbers?

조회 수: 3 (최근 30일)
riki singh
riki singh 2022년 10월 9일
편집: dpb 2022년 10월 9일
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에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by