MATLAB: How to extract the same field from diferent struct files with names like AAA_1, AAA_2… AAA_n

extraction of fieldsstruct arrays

Hi everyone!
I'm using a research app (ScanImage) that creates many struct files with names of the kind A_1, A_2, A_3 … A_n. I want to extract the field 'data' from each one of this struct files and place it in a cell or simply create the matrix A1, A2, A3 with only the data from 'data'.
A1{1,1} = A_1.data; A1{1,2} = A_2.data; etc. This will create the cell array, but I wanna do it will all the files at once!
I've trying to use this loop, but it's not working yet:
fileFolder = fullfile('location of your files in your PC');
dirOutput = dir(fullfile(fileFolder,'A_*.mat'));
fileNames = {dirOutput.name};
numFrames (1) = numel(fileNames);
ALLDATA {1,1} = A_1.data;
for cidx = 2:numFrames
ALLDATA {1,cidx} = fileNames(cidx);
end
I end up by having a cell array with the text of the name of each file, but not the real data or even the struct array pasted there in the cell array.
I would pretty much appreciate your help!
Thanks!

Best Answer

I suspect that ScanImage is badly written and saves variables (e.g. structures) with a different name in each .mat file. This matches your example "A1{1,1} = A_1.data; A1{1,2} = A_2.data;", although is a bit unclear in your explanation. In any case, as long as there is only one variable (i.e. structure) per file then this can be resolved by simply ignoring the name of the variable:
D = 'path to the directory where the files are saved';
S = dir(fullfile(D,'A*.mat'));
N = numel(S);
C = cell(1,N);
for k = 1:N
T = load(fullfile(D,S(k).name));
T = struct2cell(T);
C{k} = T{1}.data;
end