Matlab arrayfun with array of elements that are [1x4 struct] -


i have array, 'my_structures_array', single row , n columns. each element [1x4 struct]. want extract numeric valued 'thisfield' each structure of each [1x4 struct] element.

the result looking 4xn array of values each 'thisfield' value, each row in result corresponds colum in [1x4 struct].

the code using this:

arrayfun(@(x) (x.thisfield),   my_structures_array); 

matlab returns error

attempt reference field of non-structure array. 

if put following in command line,

my_structures_array{1} 

i list of of fields of [1x4 struct].

if put in command line,

my_structures_array{1}.thisfield 

i 4 answers, this:

ans =         1   ans =         1   ans =         1   ans =         0 

if @ size

size(my_structures_array{1}.thisfield) 

matlab says "error using size", see not array. i'm not sure is.

i not sure how proceed 4xn array looking for.

update

output command my_structures_array returns row array of [1x4 struct].

output whos my_structures_array{1} returns nothing

output whos my_structures_array returns:

 name                     size               bytes  class    attributes   my_structures_array      1x103            1371136  cell       

output whos my_structures_array{1}.thisfield returns nothing

output my_structures_array{1}.thisfield shown in original post.

the fact you're accessing my_structures_array my_structures_array{1} indicates it's cell array, i'll answer based on that.

say have my_structures_array cell array of struct arrays:

my_structures_array = {[1x4 struct], [1x4 struct], [1x4 struct]} 

it contains n elements (here n = 3). each element struct array 4 elements , various fields. want extract value of field foo, contains single number.

out = zeros(4, n);  = 1 : n     out(:, it) = [my_structures_array{it}.foo]; end 

out(i, j) contains value of my_structures_array{j}(i).foo

edit:

using arrayfun():

out = arrayfun(@(x) x.foo, cell2mat(my_structures_array')') 

this converts cell array of struct arrays 2d struct array, extracts field foo each element.


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -