简体   繁体   中英

Matlab: read complex numbers from text file

I have a text file containing a column of 512 complex numbers:

    1.065628906250000000e+05+2.257825312500000000e+05j
    -1.760229375000000000e+05+1.983590781250000000e+05j
     -2.912515312500000000e+05+-3.984878515625000000e+04j
     -1.352125156250000000e+05+-2.334627812500000000e+05j
     1.342380781250000000e+05+-1.729340312500000000e+05j
     2.412725312500000000e+05+6.533162500000000000e+04j
     9.673526562500000000e+04+2.301303906250000000e+05j
     -1.395607343750000000e+05+1.602902187500000000e+05j
     -2.091153281250000000e+05+-7.081546093750000000e+04j
     -5.124144921875000000e+04+-2.195292500000000000e+05j
     1.513754218750000000e+05+-1.365697968750000000e+05j
     2.135251718750000000e+05+8.967863281250000000e+04j
     6.891373437500000000e+04+2.228926250000000000e+05j
     -1.180648906250000000e+05+1.324545156250000000e+05j
     -1.687760468750000000e+05+-7.878281250000000000e+04j
     -2.107323242187500000e+04+-2.112779843750000000e+05j
     1.451967500000000000e+05+-1.367522812500000000e+05j
     1.819310781250000000e+05+7.434533593750000000e+04j
     4.500193750000000000e+04+1.951718906250000000e+05j
     -1.208624140625000000e+05+1.250783046875000000e+05j
     -1.320503906250000000e+05+-8.381024218750000000e+04j
     4.598049316406250000e+03+-2.092511093750000000e+05j
     1.398188750000000000e+05+-1.318195156250000000e+05j
     1.370294375000000000e+05+6.731912500000000000e+04j
     -2.108746093750000000e+04+1.740979375000000000e+05j
     -1.650594218750000000e+05+8.201840625000000000e+04j
     -1.426277187500000000e+05+-1.057813437500000000e+05j

How can I read this into Matlab? Using the code:

fileID = fopen('y.txt','r');
A = textscan(fileID,'%f');
fclose(fileID);
A{1}

I get the output:

>> A{1}

ans =

   1.0e+05 *

   1.0656 + 2.2578i
  -1.7602 + 1.9836i
  -2.9125 + 0.0000i

Why does Matlab only read in the first 3 rows and why does it get the complex value for row 3 incorrect (0.000i rather than -3.98e04i)?

This is caused by - being written as +- in y.txt . One way to get around this problem is to read the data as a cell array of character vectors and then use str2double , which takes care of +- as - , to convert that to the double class.

fileID = fopen('y.txt','r');
A = textscan(fileID,'%s');
fclose(fileID);
A = str2double(A{1});

Result:

>> A

A =

   1.0e+05 *

   1.0656 + 2.2578i
  -1.7602 + 1.9836i
  -2.9125 - 0.3985i
  -1.3521 - 2.3346i
   1.3424 - 1.7293i
   2.4127 + 0.6533i
   0.9674 + 2.3013i
  -1.3956 + 1.6029i
  -2.0912 - 0.7082i
  -0.5124 - 2.1953i
   1.5138 - 1.3657i
   2.1353 + 0.8968i
   0.6891 + 2.2289i
  -1.1806 + 1.3245i
  -1.6878 - 0.7878i
  -0.2107 - 2.1128i
   1.4520 - 1.3675i
   1.8193 + 0.7435i
   0.4500 + 1.9517i
  -1.2086 + 1.2508i
  -1.3205 - 0.8381i
   0.0460 - 2.0925i
   1.3982 - 1.3182i
   1.3703 + 0.6732i
  -0.2109 + 1.7410i
  -1.6506 + 0.8202i
  -1.4263 - 1.0578i

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM