Structures
조회 수: 3 (최근 30일)
이전 댓글 표시
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
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
채택된 답변
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
댓글 수: 3
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
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
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
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 Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!