5 views (last 30 days)
Charles on 13 Aug 2011
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;
I have errors like : Improper index matrix reference and other errors, Cell contents reference from a non-cell array object.
Charles on 13 Aug 2011
Thanks, I understand the question is poorly phrased and would make some corrections

Sign in to comment.

Accepted Answer

Oleg Komarov
Oleg Komarov on 13 Aug 2011
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})
This is a complete reference to manipulate structures: structures
Oleg Komarov
Oleg Komarov on 14 Aug 2011
How are the variables s and t being created or how are they stored?
If they're stored all in a .mat file then you could:
data = load(myfile.mat)

Sign in to comment.

More Answers (1)

Jan on 13 Aug 2011
I'm not sure if this matchs your question, but similar question appear very often here:
If you use LOAD without catching the output to a variable, the output is directly written to the current workspace. Accessing these variables is probe to errors and often causes troubles, e.g. if a locally used variable is overwritten.
Therefore it is much safer and easier to use LOAD with catching the output:
data = load('File.mat');
field = fieldnames(data);
fprintf(' %s\n', field{:});
Jan on 13 Aug 2011
"I have variable" and "I have a 'file'" is not clear. What is the type andthe contents of "files"? Please post the code completely by editing the original question - *not* as comment or answer. And append a complete copy of the error message, because "and other error" is not exact enough to give an advice.

Sign in to comment.


Find more on Cell Arrays in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!