Borrar filtros
Borrar filtros

Structures

4 visualizaciones (últimos 30 días)
Charles
Charles el 13 de Ag. de 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;
end
I have errors like : Improper index matrix reference and other errors, Cell contents reference from a non-cell array object.
  2 comentarios
Oleg Komarov
Oleg Komarov el 13 de Ag. de 2011
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 el 13 de Ag. de 2011
Thanks, I understand the question is poorly phrased and would make some corrections

Iniciar sesión para comentar.

Respuesta aceptada

Oleg Komarov
Oleg Komarov el 13 de Ag. de 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})
end
This is a complete reference to manipulate structures: structures
  3 comentarios
Jan
Jan el 14 de Ag. de 2011
@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 el 14 de Ag. de 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)
then
for...
data.(s{n})...
end

Iniciar sesión para comentar.

Más respuestas (1)

Jan
Jan el 13 de Ag. de 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{:});
disp(data.field{1});
  2 comentarios
Charles
Charles el 13 de Ag. de 2011
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 el 13 de Ag. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Structures en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by