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

3 次查看(过去 30 天)
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
___________________________

采纳的回答

Walter Roberson
Walter Roberson 2021-9-28
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 个评论
Walter Roberson
Walter Roberson 2021-9-28
Oh, I see a correction needed:
mats1 = arrayfun(@(S) sscanf(S.mat, '%f', [3 3]).', blocks, 'uniform', 0)
Otherwise the matrices come out transposed.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by