How to solve this errror about reading in complex numbers?

1 view (last 30 days)
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.

Answers (2)

dpb
dpb on 9 Oct 2022
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 Comments
riki singh
riki singh on 9 Oct 2022
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 on 9 Oct 2022
Edited: dpb on 9 Oct 2022
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!"

Sign in to comment.


Image Analyst
Image Analyst on 9 Oct 2022
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

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!