Parse through .txt file to create vector
6 views (last 30 days)
Show older comments
Jennifer Good
on 3 Nov 2023
Commented: Star Strider
on 3 Nov 2023
I have a .txt file of STM information that I need to create vectors for. The data comes out (see below) from a fortan code I have on VS and I can import as a .txt file.
I need a (n,1) vector
Each row in .txt file has 6 numbers to input into vector
0.185167780E+010.530606201E-020.255950360E-010.111319395E+010.795404102E+03-.200248462E+02
-.271809819E+020.598834230E+00-.804603588E+000.313665119E+03-.253884963E+050.628079535E+03
0.692778749E+000.978285945E-020.102006866E+01-.836624219E+010.647731533E+030.705167228E+01
0.291288996E-010.430243554E-030.863493621E-030.664441117E+000.271787718E+02-.671645792E+00
0.102264114E-020.109628329E-050.298501683E-04-.451697007E-020.195425110E+01-.231592002E-01
0.843485726E-030.120234978E-04-.636165956E-05-.100614284E-010.785839881E+000.980295691E+00
For example row 1, I need first 6 elements of vector to be (each number string has exactly 15 characters)
0.185167780E+01
0.530606201E-02
0.255950360E-01
0.111319395E+01
0.795404102E+03
-.200248462E+02
Thanks for any help
0 Comments
Accepted Answer
Star Strider
on 3 Nov 2023
Use the fixedWidthImportOptions function and readmatrix (since there are apparently no header lines or variable names) —
opts = fixedWidthImportOptions('NumVariables',6, 'VariableWidths',[15 15 15 15 15 15], 'DataLines',1);
% T1 = readtable('JG_20231103.txt', opts)
A1 = readmatrix('JG_20231103.txt', opts)
format longE
A1 = str2double(A1)
FirstRow = A1(1,:)
FirstRowTransposed = FirstRow.'
I created the file by pasting the original matrix into Notepad and saving it as a .txt file.
.
2 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!