read and store data from struct file during each iteration in the for loop

5 ビュー (過去 30 日間)
Hi,
I have a for loop as shown. During each iteration the will store entire output in the struct file named Measurements. (file attached here)
From this struct file file, I need to read and store only 'Centroid', 'Eccentricity', 'EquivDiameter' in the sepearte array.
for i = 1:1:1000
% some operation
Measurements = some operation
end

採用された回答

Star Strider
Star Strider 2024 年 5 月 4 日
I am not certain what result you want.
Try this —
% load('matlab')
% whos('-file', 'matlab')
%
% Measurements
fields = {'Centroid', 'Eccentricity', 'EquivDiameter'};
files = dir('*.mat');
NrFiles = numel(files);
for k = 1:NrFiles
LD = load(files(k).name);
MeasTable = struct2table(LD.Measurements);
VN = MeasTable.Properties.VariableNames;
ColNrs = find(ismember(VN,fields));
ForFile = MeasTable(:,ColNrs);
writetable(ForFile,sprintf('NewTable%04d.txt',k))
end
readtable('NewTable0001.txt')
ans = 8x4 table
Centroid_1 Centroid_2 Eccentricity EquivDiameter __________ __________ ____________ _____________ 268.6 397.8 0.76509 2.5231 333.8 643.5 0.33229 9.4407 339.86 403.57 0.54188 13.303 355.02 557.21 0.66171 34.761 355.85 445.2 0.47857 34.943 356.29 744.16 0.35417 35.396 353.46 806.41 0.25141 26.511 360.73 838.97 0.44367 28.702
If all the .mat files have the same internal structure, then ‘MeasTable’ also will, and some of the lines in the loop can be defined prior to it.
.
  20 件のコメント
Turbulence Analysis
Turbulence Analysis 2024 年 5 月 7 日
Okay, Thanks a lot for the details!
Star Strider
Star Strider 2024 年 5 月 7 日
As always, my pleasure!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by