How to access and use specific data from a structure

4 visualizaciones (últimos 30 días)
Artu
Artu el 25 de Jun. de 2012
Hello there!
So have a structure 1x1 with 10 (8760x1) elements. I want to create a script to access to each of these 10 groups of data and I want to evaluate different functions and in order to get a vector with the result of the evaluated function.
For example, I want to access the data to get the maximum value at each time, so I want to get a vector with ten elements showing me the maximum value of each group of data.
So "Graph" is the name of the structure, and "Curve'n' " is each of the 10 arrays that I need to evaluate. I have used this and it works but I find it not very practical because I have to write the whole expression down
m(i)=max(Graph.Curve1);
m(i+1)=max(Graph.Curve2);
m (i+2)=max(Graph.Curve3);
m (i+3)=max(Graph.Curve4); ...... and so on till 10
I have tried
for i=1:10;
m(i)=max(Graph.Curve"i");
end
m
But it doesn't work.
Is there a more practical way to do this?
Thank you very much.
Best regards.
  1 comentario
Walter Roberson
Walter Roberson el 25 de Jun. de 2012
http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F

Iniciar sesión para comentar.

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 25 de Jun. de 2012
m = structfun(@max,Graph);

Más respuestas (3)

Tom
Tom el 25 de Jun. de 2012
You can avoid using a loop altogether:
m=cellfun(@max,struct2cell(Graph));

grapevine
grapevine el 25 de Jun. de 2012
Try to use this function : eval Use my code as example
Graph.Curve1=[1 2]
max(eval(['Graph.Curve',num2str(1)]))
good luck
  3 comentarios
grapevine
grapevine el 26 de Jun. de 2012
i don't get it
why not?
Using eval hurts someone, doesn't it?
http://www.mathworks.fr/matlabcentral/answers/?search_submit=answers&query=eval+problem&term=eval+problem
Artu
Artu el 27 de Jun. de 2012
I am new on this programming thing so I will be careful if EVAL is not a very reliable function. Thanks.

Iniciar sesión para comentar.


Jan
Jan el 25 de Jun. de 2012
Graph.Curve1 = [1 2];
max(Graph.(sprintf('Curve%d', 1)))
This method is called "dynamic fieldnames". In opposite to the evil eval it is safe, fast, easy to debug and let Matlab to process the code efficiently.
It is a good programming practice not to include indices in the names of variables. Obviously indices are a good method to implement indices, e.g.: Graph.Curve{1}, Graph.Curve{2}, ...
  2 comentarios
Image Analyst
Image Analyst el 25 de Jun. de 2012
Artu you can do it this way, but which way do you think is easier for someone else to understand when it comes time for them to take over your code, or when you look at it a year from now, dynamic field names, or the way you did it at first? If you gave Tom's, andrei's, Jan's, and your code to your classmate, which would he understand? Not to say that dynamic field names don't have their place, but come on, it's only 10 lines of code - not that many. If you had hundreds of them then that might be a different matter. Not to take anything away from Jan's clever solution, but personally I'd vote in favor of the way you originally did it as being the way that is easier to understand. For me, I always keep in mind maintainability and not just compactness when writing code. I'm probably in the minority but that's just my 2 cents...
Artu
Artu el 27 de Jun. de 2012
You are right. It's always better to use the way we can understand easier. Maybe we worry too much about the compactness thing. I will keep your suggestion on mind. Thanks

Iniciar sesión para comentar.

Categorías

Más información sobre Programming 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