Cellfun with Structfun as function?

╄→гoц情女王★ 提交于 2019-12-25 10:02:18

问题


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.


回答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);


来源:https://stackoverflow.com/questions/31814785/cellfun-with-structfun-as-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!