Read matrices that are separated by underscores in a text file.

2 visualizzazioni (ultimi 30 giorni)
I have a large text file that has two header lines followed by a long list of 3x3 matrices that are separated from one another by underscores. Here is a small portion of the file I am using. Don't mind that these matrices are all the same. There is a second set of matrices which are different.
I am trying to figure out how to read these into the work space iteratively to perform calculations with them. I will be doing the same with the other set of matrices which has an identical format. The calculations will be performed between the two matrices.
How can I reference and pull specific lines of the text file in a for loop, while skipping the underscore seperators?
RotmA
--------------------------
0.999985 0.000046 0.005519
0.000046 0.999863 -0.016557
-0.005519 0.016557 0.999848
___________________________
0.999985 0.000046 0.005519
0.000046 0.999863 -0.016557
-0.005519 0.016557 0.999848
___________________________
0.999985 0.000046 0.005519
0.000046 0.999863 -0.016557
-0.005519 0.016557 0.999848
___________________________
0.999985 0.000046 0.005519
0.000046 0.999863 -0.016557
-0.005519 0.016557 0.999848
___________________________
0.999985 0.000046 0.005519
0.000046 0.999863 -0.016557
-0.005519 0.016557 0.999848
___________________________
0.999985 0.000046 0.005519
0.000046 0.999863 -0.016557
-0.005519 0.016557 0.999848
___________________________
0.999985 0.000046 0.005519
0.000046 0.999863 -0.016557
-0.005519 0.016557 0.999848
___________________________
0.999985 0.000046 0.005519
0.000046 0.999863 -0.016557
-0.005519 0.016557 0.999848
___________________________

Risposta accettata

Walter Roberson
Walter Roberson il 28 Set 2021
In the below, the assignment to S1 inside isunix() is just to load in some data since I do not have your file. In your actual code, you would use the fileread() to load data into S1 instead.
Afterwards, you would not use line numbers: you would index the mats1 cell array according to which matrix number you wanted.
filename = 'first_file.txt';
if isunix()
S1 = sprintf(' RotmA \n--------------------------\n0.999985 0.000046 0.005519\n0.000046 0.999863 -0.016557\n-0.005519 0.016557 0.999848\n___________________________\n0.999985 0.000046 0.005519\n0.000046 0.999863 -0.016557\n-0.005519 0.016557 0.999848\n')
else
S1 = fileread(filename);
end
S1 =
' RotmA -------------------------- 0.999985 0.000046 0.005519 0.000046 0.999863 -0.016557 -0.005519 0.016557 0.999848 ___________________________ 0.999985 0.000046 0.005519 0.000046 0.999863 -0.016557 -0.005519 0.016557 0.999848 '
blocks = regexp(S1, '(?<mat>(-?[\d.]+\s*){8}-?[\d.]+)', 'names');
mats1 = arrayfun(@(S) sscanf(S.mat, '%f', [3 3]), blocks, 'uniform', 0)
mats1 = 1×2 cell array
{3×3 double} {3×3 double}
celldisp(mats1)
mats1{1} = 1.0000 0.0000 -0.0055 0.0000 0.9999 0.0166 0.0055 -0.0166 0.9998 mats1{2} = 1.0000 0.0000 -0.0055 0.0000 0.9999 0.0166 0.0055 -0.0166 0.9998
  3 Commenti
Walter Roberson
Walter Roberson il 28 Set 2021
Oh, I see a correction needed:
mats1 = arrayfun(@(S) sscanf(S.mat, '%f', [3 3]).', blocks, 'uniform', 0)
Otherwise the matrices come out transposed.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by