フィルターのクリア

How can I read a complex number vector?

7 ビュー (過去 30 日間)
André Luiz Regis Monteiro
André Luiz Regis Monteiro 2014 年 2 月 11 日
編集済み: JasonLuo 2018 年 1 月 25 日
Hi, I have this problem. I'm trying to read a complex number vector from a txt file using this structure:
fid=fopen ('VarName4.txt','r');
acorrente=fscanf(fid,'%f');
fclose (fid);
VarName4.txt is a vector 1000x1 like this:
[0.0141451080390089-0.00224036502008852i
0.0141451080390089-0.00224036502008852i
0.0141451080390089-0.00224036502008852i
0.0141451080390089-0.00224036502008852i...]
Using the structure before, the answer is:
acorrente =
0.0141
-0.0022
Why it doesn't work? Could someone teach me? Thanks a lot!
  2 件のコメント
José-Luis
José-Luis 2014 年 2 月 11 日
It doesn't work because you are telling fscanf to expect a floating point number. Fscanf won't read in imaginary numbers.
André Luiz Regis Monteiro
André Luiz Regis Monteiro 2014 年 2 月 11 日
Hi José-Luis. So, How can I do? thanks.

サインインしてコメントする。

採用された回答

Mischa Kim
Mischa Kim 2014 年 2 月 11 日
編集済み: Mischa Kim 2014 年 2 月 11 日
André Luiz, this should do:
fid = fopen ('VarName4.txt','r');
ii = 1;
while ~feof(fid)
acorrente(ii, :) = str2num(fgets(fid));
ii = ii + 1;
end
fclose(fid);
...read each line of the text file, convert it from string to number and save it in the matrix. The loop will keep reading until the end of the file ( feof ) is reached.
  3 件のコメント
André Luiz Regis Monteiro
André Luiz Regis Monteiro 2014 年 2 月 11 日
編集済み: André Luiz Regis Monteiro 2014 年 2 月 11 日
Hi Mischa Kim. Thank you for your attention. It worked! But the numbers are still being cut. Could you help again? Thanks.
original: 0.0141451080390089-0.00224036502008852i answer: 0.0141 - 0.0022i I need a complete number. Thanks a lot.
Mischa Kim
Mischa Kim 2014 年 2 月 11 日
編集済み: Mischa Kim 2014 年 2 月 11 日
That's a formatting setting, the number is saved correctly. In the MATLAB command window put
format long

サインインしてコメントする。

その他の回答 (1 件)

JasonLuo
JasonLuo 2018 年 1 月 25 日
編集済み: JasonLuo 2018 年 1 月 25 日

カテゴリ

Help Center および File ExchangeLow-Level File I/O についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by