0

My data are in the form of a cell array of structs. I am trying to implement a cellfun call that takes structfun as its function that, at the end of the day, will resize all of the vectors in the struct to the passed-in size. E.g. I have a 4-cell array each with a struct that contains one vector (going to be multiple vectors once I figure this out), and I want to resize each vector from index1 to index2

fun = function(foo, index1, index2) 
cellfun(@structfun(@(x) x(index1:index2), foo, 'UniformOutput',false), foo, 'UniformOutput', false)

Do I have to do a loop and replace the first "foo" with "foo(i)" to be able to reach all the cells? Thanks in advance.

call-in-co
  • 281
  • 2
  • 11
  • What does your cell array of structs look like? Why do you have a cell array of structs in the first place? – sco1 Aug 04 '15 at 16:41
  • @excaza I have that setup because that's how my boss stores the results of the experiment. Can't change that, unfortunately. My cell array, foo, is a "1x4 struct array with fields: a b c d" is what Matlab says. Each of a,b,c,d, is a 10x1 vector. P.S. I persist with an array of structs instead of a struct of arrays partly because of [this](http://matlabrecipes.com/structures-of-arrays-vs-arrays-of-structures/) – call-in-co Aug 04 '15 at 16:47
  • 1
    You cannot inline structfun like that. You need to create a temporary function handle which calls structfun and use that in cellfun. sf = @(y) structfun(y(1:2), y, 'UniformOutput',false); cellfun(sf, ... – Navan Aug 04 '15 at 16:55
  • @Navan thank you very much. – call-in-co Aug 04 '15 at 17:10
  • If that helped, I will post my comment as answer for future reference. – Navan Aug 04 '15 at 17:28

1 Answers1

1

You cannot inline structfun like that. You need to create a temporary function handle which calls structfun and use that in cellfun.

sf = @(y) structfun(@(x) x(1:2), y, 'UniformOutput',false); 
cellfun(sf, foo, 'UniformOutput', false);

You can do this in one line as below. But it is better to keep this in two lines for readability.

cellfun(@(y) structfun(@(x) x(1:2), y, 'UniformOutput',false), foo, 'UniformOutput', false);
Navan
  • 4,407
  • 1
  • 24
  • 26
  • 1
    Your code has issues. The first argument to `structfun` need to be a function handle. You can still put everything on one line... – horchler Aug 04 '15 at 18:59