A subset of the values ​​of each field in the MATLAB structure

have a structure called data with fields data1 data2, data3, data4, in which there are many elements. But I want to break the variable newdata, which has the same fields, but only the first 100 elements of each field in it. Does anyone know a quick way to do this without a loop or brute force method?

                           data1: [3744x1 double]
                           data2: [3744x1 double]
                           data3: [3744x1 double]
                           data4: [3744x1 double]
+3
source share
1 answer

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]
+8
source

Source: https://habr.com/ru/post/1788366/


All Articles