Clear Filters
Clear Filters

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

2 views (last 30 days)
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

Accepted Answer

Star Strider
Star Strider on 4 May 2024
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 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!