MATLAB Answers

0

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

Asked by Diego Alvarez on 11 Apr 2019
Latest activity Commented on by Diego Alvarez on 18 Apr 2019
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!

  2 Comments

"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.

2 Answers

Answer by Stephen Cobeldick on 18 Apr 2019
Edited by Stephen Cobeldick on 18 Apr 2019
 Accepted 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

Answer by Walter Roberson
on 11 Apr 2019
Edited by Walter Roberson
on 11 Apr 2019

fileNames = fullfile(fileFolder, {dirOutput.name});
numFrames = length(fileNames);
ALLDATA = cell(1, numFrames);
for cidx = 1 : numFrames
filestruct = load(fileNames{cidx}, 'data');
ALLDATA{1, cidx} = filestruct.data;
end

  5 Comments

Still not working and I think this is not the way of solving it. Gonna check other alternatives now.
Thanks Walter Roberson!!!
"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.
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.
Thanks!!!!

Sign in to comment.