Accessing Values From Structure or Cell Aray

Hey Guyz..
I am here with another question,The problem I am facing is the accessing and utilizing the data stored in cell Array or Struct. The Project I am currently working on the calculation of centroid values stored in the struct.
I want to create a function that receives from desired struct,uses the point distance formula and calculate the distances for each and every centroid distances of whole image. I have also attached the image of that struct values....
Thanks in Advance !!!!

2 Comments

Which part of the problem are you actually asking the question about?
g(7).Centroid
allows you to access e.g. the Centroid of the 7th element of a struct, but I assume you know that already. Also a for loop or arrayfun can work on the full array of structs. So which part of 'accessing values from structure or cell array' is the problem?

Sign in to comment.

 Accepted Answer

centroidVals = sscanf( g(i).Centroid, '[%f,%f]' )
will give you the i centroid as just a pair of doubles.
Your struct array only has one field so I assume you mean the first two elements of the array rather than fields 1 and 2.
centroidVals = sscanf( g(1:2).Centroid, '[%f,%f]' )
centroidVals = sscanf( g.Centroid, '[%f,%f]' )
will give you the first two centroid values or all centroid values (respectively).
The result will be in a vector list, each centroid starting at an odd index, but it can easily be reshaped or manipulated into another format.

5 Comments

Thank You again Adam for your kind response. Let me make it more obvious and understandable ,as you can see there are almost 1384 fields in a given struct. Firstly, I want to extract the first 2 fields i-e 1 and 2,then I want to extract both values in field 1 and field 2 respectively. Basically i want to apply point distance formula on them i-e distance = sqrt((x2-x1)^2+(y2-y1)^2);..
I hope you will help me with that too
Your screen shot above seems to be significantly different from the problem you are describing unless you are just getting mixed up between fields and elements of a struct array.
The screenshot above shows an array of 1879 structures, each with just a single field. A structure with 1384 fields would certainly be large.
Adam I have posted an image regarding my problem,I wish You will get the point this time...My problem statement is given in there...
Sir Adam,waiting for your answer !!!!
Sorry, I can't spare the time to look deep into problems from paper. The original question from the heading was simply regarding accessing values from structures or cell arrays without requiring specific problem knowledge.

Sign in to comment.

More Answers (1)

Thank you Adam for your kind response and help. As you can see that each field has two values in it separated by "COMMA". I want to get the values from first two fields,let say Field 1 and Field 2; this give us 4 values. I want to a block of code that the values from first two fields passed in a function,from their it get calculated. Could You Help with that two .
Thank you Again in advance

Asked:

on 13 May 2015

Commented:

on 27 May 2015

Community Treasure Hunt

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

Start Hunting!