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

3 ビュー (過去 30 日間)
Michael Costa
Michael Costa 2021 年 9 月 28 日
コメント済み: Michael Costa 2021 年 9 月 28 日
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.
Michael Costa
Michael Costa 2021 年 9 月 28 日
Oh nice catch. Thank you

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by