Read matrices that are separated by underscores in a text file.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Michael Costa
am 28 Sep. 2021
Kommentiert: Michael Costa
am 28 Sep. 2021
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
___________________________
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 28 Sep. 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
blocks = regexp(S1, '(?<mat>(-?[\d.]+\s*){8}-?[\d.]+)', 'names');
mats1 = arrayfun(@(S) sscanf(S.mat, '%f', [3 3]), blocks, 'uniform', 0)
celldisp(mats1)
3 Kommentare
Walter Roberson
am 28 Sep. 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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Environment and Settings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!