Structures

3 次查看(过去 30 天)
Charles
Charles 2011-8-13
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.
  2 个评论
Oleg Komarov
Oleg Komarov 2011-8-13
Please rephrase ("I want to analyse each of the file by giving a text file containing the name of the structures that I want to analyse") and be more clear, maybe with an example of what you have and waht you'd like to achieve: http://www.mathworks.com/matlabcentral/answers/6200-tutorial-how-to-ask-a-question-on-answers-and-get-a-fast-answer
Charles
Charles 2011-8-13
Thanks, I understand the question is poorly phrased and would make some corrections

请先登录,再进行评论。

采纳的回答

Oleg Komarov
Oleg Komarov 2011-8-13
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
  3 个评论
Jan
Jan 2011-8-14
@Charles: Please do not tell us, that "it doesn't work", but post a copy of the error message. Otherwise we waste too much time with guessing.
If fnames contains file names, load the files at first:
data = load(fnames{i}), disp(data.hello)
Oleg Komarov
Oleg Komarov 2011-8-14
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)
then
for...
data.(s{n})...
end

请先登录,再进行评论。

更多回答(1 个)

Jan
Jan 2011-8-13
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{:});
disp(data.field{1});
  2 个评论
Charles
Charles 2011-8-13
I guess I posed my question poorly.
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 error.
Jan
Jan 2011-8-13
"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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by