MATLAB: Structures

structures

I have loaded data that contain structures. The structures are of the form I have variable : struct1, struct2, struct3…. I have a 'file' with the list of these variables and want to manipulate the data contained in the structures with a for-loop.
Here is what I did:
for i = 1:numel(files)
data = files(i).data or data{1}.data
some code;
end
I have errors like : Improper index matrix reference and other errors, Cell contents reference from a non-cell array object.

Best Answer

You could have the following structure:
s.myfield = 10;
s.another = 20;
s.hello = 30;
Now suppose you have a list with the fieldnames which also happens to be the function to retrieve them from a structure:
fnames = {'myfield','another','hello'};
(Note that I didn't call the list with the name of the function)
Now you can manipulate the structure using dynamic fieldname indexing:
for n = 1:numel(fnames)
tmp = s.(fnames{n});
% do stuff with tmp or directly with s.(fnames{n})
end
This is a complete reference to manipulate structures: structures