import complex column into matlab
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentario
dpb
el 27 de Dic. de 2014
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.
Respuestas (2)
per isakson
el 27 de Dic. de 2014
Editada: per isakson
el 27 de Dic. de 2014
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
 
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
0 comentarios
Matthew Parry
el 7 de Nov. de 2019
In R2019a or later you can use
data = readmatrix(fname);
0 comentarios
Ver también
Categorías
Más información sobre Text Files en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!