import complex column into matlab

조회 수: 5 (최근 30일)
atib0539
atib0539 2014년 12월 27일
답변: Matthew Parry 2019년 11월 7일
Hi everybody,
I have a two-column data that I want to import into the workplace. The two columns are separated by comma, for all real numbers I could use the following code,
data = textread('data.txt', '', 'delimiter', ',');
x = data(1:(length(data(:,1))-1),1);
y = data(1:(length(data(:,2))-1),2);
But now I have data after FFT like this, where the second column is complex,
0,-7.45678527326516E-7
0.0123532704572966,-9.13085574558244E-7 - 3.24050470823079E-7i
0.0247065409145915,-1.78617729703184E-6 + 9.66937131199859E-7i
0.0370598113718881,3.11409269891353E-6 - 1.40731487179325E-5i
0.0494130818291847,-8.23979324344331E-6 + 4.94108233557163E-5i
0.0617663522864795,3.95217467868805E-5 - 1.00035273201451E-4i
0.0741196227437761,-4.26665707593228E-5 + 7.19117803282971E-5i
How can I import it? The file can be saved as .txt, .csv, or .dat.
Thanks a lot!!
  댓글 수: 1
dpb
dpb 2014년 12월 27일
Can you modify the process that writes the file any at all? If you would at least
a) Include the explicit zero imaginary part of the first row, and
b) eliminate the space between the sign and the numeric value for the imaginary component
it would be simpler to parse with standard formatting descriptors.

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

답변 (2개)

per isakson
per isakson 2014년 12월 27일
편집: per isakson 2014년 12월 27일
AFAIK: The complex number in the file need to be written without spaces between the real and the imaginary parts. However, I failed to find that stated in the documentation.
One way to read your file is to
  • read the entire file to one character string
  • remove the spaces
  • parse the string with the function, textscan
Try
>> cac = cssm()
cac =
[7x1 double] [7x1 double]
>> cac{1}
ans =
0
0.0124
0.0247
0.0371
0.0494
0.0618
0.0741
>> cac{2}
ans =
1.0e-03 *
-0.0007 + 0.0000i
-0.0009 - 0.0003i
-0.0018 + 0.0010i
0.0031 - 0.0141i
-0.0082 + 0.0494i
0.0395 - 0.1000i
-0.0427 + 0.0719i
>>
where
function cac = cssm()
str = fileread( 'cssm.txt' );
str = strrep( str, ' ', '' );
cac = textscan( str, '%f%f', 'Delimiter', ',' );
end
and where cssm.txt contains
0,-7.45678527326516E-7
0.0123532704572966,-9.13085574558244E-7 - 3.24050470823079E-7i
0.0247065409145915,-1.78617729703184E-6 + 9.66937131199859E-7i
0.0370598113718881,3.11409269891353E-6 - 1.40731487179325E-5i
0.0494130818291847,-8.23979324344331E-6 + 4.94108233557163E-5i
0.0617663522864795,3.95217467868805E-5 - 1.00035273201451E-4i
0.0741196227437761,-4.26665707593228E-5 + 7.19117803282971E-5i
&nbsp
Or use CollectOutput and read to one matrix
>> cac = cssm()
cac =
[7x2 double]
>> cac{1}
ans =
0.0000 + 0.0000i -0.0000 + 0.0000i
0.0124 + 0.0000i -0.0000 - 0.0000i
0.0247 + 0.0000i -0.0000 + 0.0000i
0.0371 + 0.0000i 0.0000 - 0.0000i
0.0494 + 0.0000i -0.0000 + 0.0000i
0.0618 + 0.0000i 0.0000 - 0.0001i
0.0741 + 0.0000i -0.0000 + 0.0001i
where
function cac = cssm()
str = fileread( 'cssm.txt' );
str = strrep( str, ' ', '' );
cac = textscan( str, '%f%f', 'Delimiter',',', 'CollectOutput',true );
end

Matthew Parry
Matthew Parry 2019년 11월 7일
In R2019a or later you can use
data = readmatrix(fname);

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by