MATLAB Answers

Mariam Sheha

How to convert 'Structure' ?!

Asked by Mariam Sheha
on 29 Jun 2013

Hi everybody;

I am trying to get feature value out of structure array..

for example:

glcms = graycomatrix(CG,'Offset',[0 1]);

Cont_RGB = graycoprops(glcms,'Contrast');


F= Contrast: 0.2546 % i wana to remove word contrast and get the number only!

How can i get the number?

Thank you


3 Answers

Answer by Walter Roberson
on 30 Jun 2013
 Accepted answer

If your

Cont_RGB = graycoprops(glcms,'Contrast');

is returning a Cont_RGB which is a structure array, and you want to extract the field named "Contrast" from the first member of that structure array, you would use


and in the cast where the structure array only has a single member (so Cont_RGB(2) does not exist) then you can abbreviate that to


In the above, the period is part of the syntax.

If Cont_RGB is a structure array, so Cont_RGB(2).Contrast and so on exist, and you are trying to get a vector equivalent to

[Cont_RGB(1).Contrast Cont_RGB(2).Contrast Cont_RGB(3).Contrast ....]

then use


In this, the [] and the period are part of the syntax.

  1 Comment

yes, it works thanks alo0o0ot :)

Answer by Matthew Eicholtz on 29 Jun 2013




can you tell me what does 'F','contrast' represent

Cont_RGB = graycoprops(glcms,'Contrast');

The "." between the variable name and "Contrast" is literal, part of the syntax.

Answer by Image Analyst
on 29 Jun 2013

theNumber = F.Contrast;


you don't understand my point...

i had already get the result like that


Contrast: 0.2546

% i want to remove word 'contrast' and get the number alone...

I had already reach it by converting structure to cell using function (_struct2cell_)

Then convert cell to matrix using function (cell2mat)

but it's about 200 feature and it's too long to pass it through 2 steps for each...

I wander if their is simple step rather than using two steps?!!

All 3 people answered with the same thing, and you accepted the third response, so I think we all knew what you meant. Hopefully you're now using Walter's answer, and are not doing unnecessary operations like converting to a cell array and then converting the cell array to a matrix. You may find it helpful to read the FAQ:

Thanks for you all replaying me,

Walter's mentioned his answer,and so i get the point once i read discussion..

yes i do using the method now instead of using useless operation..

Also, Thanks for the doc. it's helpful

Really you are all helpful and creative...:)

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!