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

5 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Star Strider
Star Strider el 4 de Mayo de 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 comentarios

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Cell Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by