M HYPE SPLASH
// general

Structure Array in Matlab

By Emma Valentine
$\begingroup$

I have created a structure array in matlab as follows:

clc;
clear all;
close all;
field1 = 'relation'; value1 = {'Brother','Sister','Grandfather'};
field2 = 'name'; value2 = {'Samuel', 'Giorgia','Alex'};
field3 = 'state'; value3 = {'LA', 'NY', 'CA' };
field4 = 'age'; value4 = [26 19 68];
family_structure = struct(field1,value1,field2,value2,field3,value3,field4,value4);

Now I need to retrieve the minimum age by using min(), however i can manually do this. Moreover,If I convert this structure array into cell array as

family_cell=struct2cell(family_structure);

How can I use the min() function to retrieve the same.

NB: I have figured it out for both structure array and cell

Input:

clc;
clear all;
close all;
family.relation= 'Brother';
family.name='Turing';
family.state = 'NY';
family.age=28
family(2).relation= 'Sister';
family(2).name='Giorgea';
family(2).state = 'LA';
family(2).age=19
family(3).relation= 'Grandfather';
family(3).name='Fiegenbaum';
family(3).state = 'CA';
family(3).age=68

Command For Structure Array:

min([family.age]')

Output: 19

Command For Cell:

family_cell=struct2cell(family);
f=cell2mat(family_cell(4,:)');
min([f(2,:)])

Output: 19

$\endgroup$ 2 Reset to default

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy