How to read txt file with complex number(with imaginary part) in matlab

62 ビュー (過去 30 日間)
NeoBeaver
NeoBeaver 2016 年 7 月 27 日
回答済み: Matthew Parry 2019 年 11 月 7 日
Dear all Any one could help me reading the txt file 'specimen1.txt' to plot the load vs extension .For instance:
Any one could help please as soon. Regards Ruming

採用された回答

John BG
John BG 2016 年 7 月 27 日
編集済み: John BG 2016 年 7 月 27 日
Hi NeoBeaver
A way to import your data is
A=textread('B field_example.txt','%s')
If you want to skip the header:
textread('B field_example.txt','%s','headerlines',5)
the header is already in
A{1:37}
but you still have to manually format it all in the way you want.
To go straight to the data:
L=zeros(1,length(A)-37)
for k=1:length(A)-37
L(k)=str2num(A{k+37})
end
Now L is 18 rows x 6 columns already complex double.
There are many more things that can be done during a text scan of this type, but bear in mind that the flexibility of cells with variable array elements, comes at a cost of less built-in functions, that have to be custom written.
So if you want to this answer developed, let me know.
NeoBeaver would you please be so kind to mark my answer as ACCEPTED ANSWER?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John
  2 件のコメント
NeoBeaver
NeoBeaver 2016 年 7 月 27 日
Hi John,
Thanks a lot for your answer!
I have a further question on this problem:
When I run your code, the reading of the file is great, the only problem is that the L matrix is not actually a 18 rows x 6 columns double, but 1x81:
Could you help me with it?
Thanks a lot!
Ruming
John BG
John BG 2016 年 7 月 28 日
sorry, I did not paste the last line
L=reshape(L,18,6);

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

その他の回答 (2 件)

Star Strider
Star Strider 2016 年 7 月 27 日
編集済み: Star Strider 2016 年 7 月 27 日
The textscan function will read the file without problems:
fidi = fopen('B field_example.txt','r');
Data = textscan(fidi, '%f%f%f%f%f%f', 'HeaderLines',6, 'CollectOutput',1);
Data = cell2mat(Data);
Look = Data(1:5,:) % Look At The First 5 Rows (Optional)
The first 2 lines:
Look =
Columns 1 through 3
-0.010491 + 0i -0.058909 + 0i -0.004124 + 0i
-0.0019969 + 0i -0.060173 + 0i -0.004124 + 0i
Columns 4 through 6
1.0928 + 3.1903i -2.2216 - 0.31198i -0.12483 + 0.046567i
1.4188 + 3.189i -2.0119 + 0.059764i -0.35796 + 0.17549i
EDIT — Your file has 108 elements, not 81. What do you want to include? How do you want to convert the matrix to a vector?
Two possibilities for converting the entire matrix to a row vector:
DataVec1 = reshape(Data, 1, []); % [Column#1' Column#2' ... ]
DataVec2 = reshape(Data', 1, []); % [Row#1 Row#2 ...]
  2 件のコメント
NeoBeaver
NeoBeaver 2016 年 7 月 27 日
Hi Star Strider,
Thanks a lot! It works perfectly.
Ruming
Star Strider
Star Strider 2016 年 7 月 28 日
My pleasure!
You can Accept my Answer if it works for you.

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


Matthew Parry
Matthew Parry 2019 年 11 月 7 日
In R2019a or later you can use
data = readmatrix(fname);

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by