How to read txt file with complex number(with imaginary part) in matlab
Show older comments
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
Accepted Answer
More Answers (2)
Star Strider
on 27 Jul 2016
Edited: Star Strider
on 27 Jul 2016
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 Comments
NeoBeaver
on 27 Jul 2016
Star Strider
on 28 Jul 2016
My pleasure!
You can Accept my Answer if it works for you.
Matthew Parry
on 7 Nov 2019
In R2019a or later you can use
data = readmatrix(fname);
Categories
Find more on Data Import and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!