Clear Filters
Clear Filters

Parse through .txt file to create vector

4 views (last 30 days)
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

Accepted Answer

Star Strider
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)
A1 = 6×6 cell array
{'0.185167780E+01'} {'0.530606201E-02'} {'0.255950360E-01'} {'0.111319395E+01'} {'0.795404102E+03'} {'-.200248462E+02'} {'-.271809819E+02'} {'0.598834230E+00'} {'-.804603588E+00'} {'0.313665119E+03'} {'-.253884963E+05'} {'0.628079535E+03'} {'0.692778749E+00'} {'0.978285945E-02'} {'0.102006866E+01'} {'-.836624219E+01'} {'0.647731533E+03'} {'0.705167228E+01'} {'0.291288996E-01'} {'0.430243554E-03'} {'0.863493621E-03'} {'0.664441117E+00'} {'0.271787718E+02'} {'-.671645792E+00'} {'0.102264114E-02'} {'0.109628329E-05'} {'0.298501683E-04'} {'-.451697007E-02'} {'0.195425110E+01'} {'-.231592002E-01'} {'0.843485726E-03'} {'0.120234978E-04'} {'-.636165956E-05'} {'-.100614284E-01'} {'0.785839881E+00'} {'0.980295691E+00'}
format longE
A1 = str2double(A1)
A1 = 6×6
1.0e+00 * 1.851677800000000e+00 5.306062010000000e-03 2.559503600000000e-02 1.113193950000000e+00 7.954041020000000e+02 -2.002484620000000e+01 -2.718098190000000e+01 5.988342300000000e-01 -8.046035880000000e-01 3.136651190000000e+02 -2.538849630000000e+04 6.280795350000000e+02 6.927787490000000e-01 9.782859450000000e-03 1.020068660000000e+00 -8.366242189999999e+00 6.477315330000000e+02 7.051672280000000e+00 2.912889960000000e-02 4.302435540000000e-04 8.634936210000000e-04 6.644411170000000e-01 2.717877180000000e+01 -6.716457920000000e-01 1.022641140000000e-03 1.096283290000000e-06 2.985016830000000e-05 -4.516970070000000e-03 1.954251100000000e+00 -2.315920020000000e-02 8.434857260000000e-04 1.202349780000000e-05 -6.361659560000000e-06 -1.006142840000000e-02 7.858398810000000e-01 9.802956910000000e-01
FirstRow = A1(1,:)
FirstRow = 1×6
1.851677800000000e+00 5.306062010000000e-03 2.559503600000000e-02 1.113193950000000e+00 7.954041020000000e+02 -2.002484620000000e+01
FirstRowTransposed = FirstRow.'
FirstRowTransposed = 6×1
1.851677800000000e+00 5.306062010000000e-03 2.559503600000000e-02 1.113193950000000e+00 7.954041020000000e+02 -2.002484620000000e+01
I created the file by pasting the original matrix into Notepad and saving it as a .txt file.
.

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!