How to read .txt file which have complex numbers and how to separate real and imaginary parts of the complex numbers?
12 ビュー (過去 30 日間)
古いコメントを表示
I have been trying to load .txt file which is attached with this post. Actually my task has two parts:
a) How I can read the .txt file which comprised of complex numbers?
Note:
I have been trying with textread command but it is assigning the real and imaginary part of the complex number in different rows. Actually I want both real and imaginary part of the complex number in a same cell.
b) How can I sepaarte the real and imaginary part and put them in separate columns?
1 件のコメント
Stephen23
2020 年 8 月 13 日
編集済み: Stephen23
2020 年 8 月 13 日
Note to future readers: according to the textscan documentation space characters surrounding the + and - characters within the complex numbers are not processed. The first of these characters causes textscan to quit after reading only two values from the file (as I showed here).
採用された回答
KSSV
2020 年 8 月 13 日
fid = fopen("smithchartall.txt") ;
S = textscan(fid,'%f %f %f\n','HeaderLines',1) ;
fclose(fid) ;
C1 = S{1} ; C2 = S{2} ; C3 = S{3} ;
% Seperate real and imaginary
C2_real = real(C2) ;
C2_imainary = imag(C2) ;
1 件のコメント
Stephen23
2020 年 8 月 13 日
編集済み: Stephen23
2020 年 8 月 13 日
Fails to read the complete file:
>> S{:}
ans =
0.1000
ans =
0.9794
ans =
Empty matrix: 0-by-1
This is due to the whitespace within the complex numbers, which causes textscan to quit.
"Do not include embedded white space in a complex number. textscan interprets embedded white space as a field delimiter."
その他の回答 (2 件)
Stephen23
2020 年 8 月 13 日
編集済み: Stephen23
2020 年 8 月 13 日
1.
Remove the space characters embedded in the complex numbers:
opt = {'MultipleDelimsAsOne',true,'HeaderLines',1};
str = fileread('smithchartall.txt');
str = regexprep(str,'(\d)\s([-+])\s(\d)','$1$2$3');
out = textscan(str,'%f%f%f',opt{:});
out = [out{:}];
2.
Separate complex numbers into real and imaginary parts:
doc real
doc imag
1 件のコメント
Stephen23
2020 年 8 月 13 日
>> out
out =
0.1000 0.9794 - 0.1131i 0.9874 - 0.1569i
0.1010 0.9793 - 0.1142i 0.9872 - 0.1584i
0.1020 0.9792 - 0.1153i 0.9869 - 0.1600i
0.1030 0.9790 - 0.1165i 0.9866 - 0.1615i
0.1040 0.9789 - 0.1176i 0.9864 - 0.1631i
0.1050 0.9787 - 0.1187i 0.9861 - 0.1646i
...
3.9950 -0.7727 - 0.5985i -0.7613 - 0.5993i
3.9960 -0.7727 - 0.5983i -0.7614 - 0.5991i
3.9970 -0.7728 - 0.5981i -0.7615 - 0.5990i
3.9980 -0.7729 - 0.5979i -0.7616 - 0.5989i
3.9990 -0.7730 - 0.5978i -0.7617 - 0.5987i
4.0000 -0.7731 - 0.5976i -0.7618 - 0.5986i
Maaz Salman
2020 年 8 月 13 日
2 件のコメント
Stephen23
2020 年 8 月 13 日
KSSV's code always fails for me, I tried on three MATLAB releases:
>> S{:} % R2009b:
ans =
0.1000
ans =
0.9794
ans =
Empty matrix: 0-by-1
>> S{:} % R2012b
ans =
0.1000
ans =
0.9794
ans =
Empty matrix: 0-by-1
>> S{:} % R2015b
ans =
0.1000
ans =
0.9794
ans =
Empty matrix: 0-by-1
What OS and MATLAB version are you using?
参考
カテゴリ
Help Center および File Exchange で Text Files についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!