MATLAB Answers

How do I extract the same field from diferent struct files with names like AAA_1, AAA_2... AAA_n?

3 views (last 30 days)
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} =; A1{1,2} =; 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 = {};
numFrames (1) = numel(fileNames);
ALLDATA {1,1} =;
for cidx = 2:numFrames
ALLDATA {1,cidx} = fileNames(cidx);
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!


Stephen Cobeldick
Stephen Cobeldick on 18 Apr 2019
"simply create the matrix A1, A2, A3 "
Do NOT do this. Dynamically defining variable names is one way that beginners force themselves into writing slow, complex, obfuscated, buggy code that is hard to debug. Read this to know more:
Using a cell array and simple, neat, very efficient indexing is a much better approach.

Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 18 Apr 2019
Edited: Stephen Cobeldick on 18 Apr 2019
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} =; A1{1,2} =;", 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;

More Answers (1)

Walter Roberson
Walter Roberson on 11 Apr 2019
Edited: Walter Roberson on 11 Apr 2019
fileNames = fullfile(fileFolder, {});
numFrames = length(fileNames);
ALLDATA = cell(1, numFrames);
for cidx = 1 : numFrames
filestruct = load(fileNames{cidx}, 'data');
ALLDATA{1, cidx} =;


Show 2 older comments
Diego Alvarez
Diego Alvarez on 17 Apr 2019
Still not working and I think this is not the way of solving it. Gonna check other alternatives now.
Thanks Walter Roberson!!!
Stephen Cobeldick
Stephen Cobeldick on 18 Apr 2019
"I think this is not the way of solving it"
But it is a reasonable solution, based on your description.
Your question is not very clear, for example MATLAB does not have anything called "struct files". MATLAB does have things called .mat files which can certainly contain structures (and also many other types of data), but "struct files" do not exist (a structure is a class of variable in MATLAB workspace, not a file format). Also it is not clear from your description what the names of the variables are inside the files (if indeed they are .mat files, which you did not tell us).
It would help if you uploaded a sample file for us to work with.
Diego Alvarez
Diego Alvarez on 18 Apr 2019
I'm uploading a .mat file.
The app generates .mat files with a root name and an index number (AD1_1, AD1_2, AD1_3, AD1_n). The data is on the field data of each .mat file. It would be easier for later operations to have all the data fields in just one cell array (ALLDATA). I can do that manually, extrating one by one and adding each data field into the ALLDATA cell array, but I think it should be possible to do all the .mat files in a folder at once.

Sign in to comment.

Sign in to answer this question.

Translated by