MATLAB batch processing structure array method

Preface

When you encounter a structure array, and you need to do some batch operations on the same field under the structure, the general method is to perform a for loop, but this does not look elegant.

Batch operation of specific fields in structure array

Here is a quick and practical method:
For example, there is a structure student

student(1).name = 'xiaomin';student(1).age = 1;
student(2).name = 'xiaozhang';student(2).age = 3;

Now you need to add 1 to the age field of each element in the structure. The general approach is to loop each element, and then perform some operations on the specific field of each element in the structure array.
Now combine the deal command and cellfun to operate the structure:

age = {
    
    };
[ age{
    
     1:length( student ) } ] = deal( student.age );
age = ( cellfun( @(x)x+1, age) );

Replace @(x)x+1 with the function handle funHandler, which is the function handle for the operation of the age element

Batch operation of the elements of the structure array that meet specific conditions

For example, batch delete those elements whose structure elements are empty:

a = struct('test',{
    
    1,[],[],3})
b = struct2cell(a);
a = cell2struct(b(~cellfun('isempty',b)),'test')

Batch operation of eligible elements of the cell array

For example, delete the empty elements in the cell array:
ParameterOut is a cell array.

ParameterOut( cellfun( @isempty, ParameterOut ) ) = [  ];

Auxiliary function

cellfun function description

Usage: A = cellfun(func,C)
applies the function func to the contents of each cell of the cell array C, one cell at a time. Then cellfun concatenates the output of func into the output array A, so for the i-th element of C, A(i) = func(C{i}). The input parameter func is a function handle of a function, this function accepts an input parameter and returns a scalar. The output of func can be any data type, as long as objects of that type can be concatenated. Array A and cell array C have the same size.

arrayfun function description

Apply function to each array element This MATLAB function applies the function func to the elements of A, one element at a time. Then arrayfun concatenates the output of func into the output array B, so for the i-th element of A, B(i) = func(A(i)). The input parameter func is a function handle of a function, this function accepts an input parameter and returns a scalar. The output of func can be any data type, as long as objects of that type can be concatenated. Arrays A and B must have the same size.

structfun function description

Apply the function to each field of the scalar structure This MATLAB function applies the function func to each field of the scalar structure S, one field at a time. Then structfun concatenates the output of func into a column vector A. The input parameter func is a function handle of a function, this function accepts an input parameter and returns a scalar. The output of func can be any data type, as long as objects of that type can be concatenated. The number of elements in A is equal to the number of fields in S.

Guess you like

Origin blog.csdn.net/qq_36320710/article/details/112248582