Effacer les filtres
Effacer les filtres

How to access and use specific data from a structure

2 vues (au cours des 30 derniers jours)
Artu
Artu le 25 Juin 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 commentaire
Walter Roberson
Walter Roberson le 25 Juin 2012
http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F

Connectez-vous pour commenter.

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 25 Juin 2012
m = structfun(@max,Graph);
  1 commentaire
Artu
Artu le 27 Juin 2012
I find this very helpful and practical. Thanks.

Connectez-vous pour commenter.

Plus de réponses (3)

Tom
Tom le 25 Juin 2012
You can avoid using a loop altogether:
m=cellfun(@max,struct2cell(Graph));

grapevine
grapevine le 25 Juin 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 commentaires
grapevine
grapevine le 26 Juin 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 le 27 Juin 2012
I am new on this programming thing so I will be careful if EVAL is not a very reliable function. Thanks.

Connectez-vous pour commenter.


Jan
Jan le 25 Juin 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 commentaires
Image Analyst
Image Analyst le 25 Juin 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 le 27 Juin 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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by