开发者

Subset of the value of each field in a MATLAB structure

say have a structure called data with fields called data1 data2, data3,开发者_高级运维 data4 that have many items in them. But i want to crate a variable newdata which has the same fields but only the first 100 items of each field in it. does anyone know a quick way to do this without a loop or a brute force method?

                           data1: [3744x1 double]
                           data2: [3744x1 double]
                           data3: [3744x1 double]
                           data4: [3744x1 double]


Use STRUCTFUN

newdata = structfun(@(x)x(1:100),data,'uniformOutput',false);

Example:

>> data = struct('a',1:10,'b',1:10);
>> newdata = structfun(@(x)x(1:3),data,'uniformOutput',false)
newdata = 
    a: [1 2 3]
    b: [1 2 3]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜