2017-09-28 85 views
1

複數我有一個包含512個複數列的文本文件:Matlab的:讀取文本文件

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 

我怎麼能看這種到Matlab?使用代碼:

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

我得到的輸出:

>> A{1} 

ans = 

    1.0e+05 * 

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

爲什麼Matlab的只有前3行讀取和爲什麼它獲得第3行不正確(0.000i複雜的價值,而不是-3.98e04i)?

回答

2

這是由-被寫爲+-y.txt中引起的。解決此問題的一種方法是將數據讀取爲字符向量的單元數組,然後使用str2double(將+-作爲-)將其轉換爲雙精度類。

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

結果:

>> 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