How can I get the min/max value of all fields in a stucture

I would like to obtain the minimum and maximum value in my structure which contains multiple fields.
structure_1.field_01
structure_1.field_02
structure_1.field_03
structure_1.field_04
I require something like-
min_val = min(struct.(:));
Is this possible using one function?
thanks in advance,
Marcus

1 Kommentar

What is in field_01? Is it a scalar, a n-d double array, something else? How about field_02? Is it the same class and size as field_01?

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 11 Okt. 2012
out = min(structfun(@(x)min(x(:)),structure_1));

3 Kommentare

+1 for structfun, I just gave basically the same answer with cellfun, but structfun is much better.
Just what I was looking for. Thanks.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Daniel Shub
Daniel Shub am 11 Okt. 2012
Assuming the contents of the fields are well behaved ...
structure_1.field_01 = rand(1, 1);
structure_1.field_02 = rand(1, 10);
structure_1.field_03 = rand(10, 1);
structure_1.field_04 = rand(10, 10);
structure_1.field_05 = rand(10, 10, 10);
min(cellfun(@(x)min(structure_1.(x)(:)), fieldnames(structure_1)))
By well behaved I mean min(structure_1.field_01(:)) needs to return a scalar double.

Kategorien

Mehr zu MATLAB finden Sie in Hilfe-Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by