fieldfun: Apply function to matching fields of structures

版本 1.0.0 (163.7 KB) 作者: George Abrahams
S = FIELDFUN(FUN,S1,...,SN) passes field values in structures S1,...,SN to function FUN and returns the result in scalar struct S.
27.0 次下载
更新时间 2022/12/29

fieldfun: Apply a function to the matching fields of structures

View on GitHub View on File Exchange

Summary

S = fieldfun(fun,S1,...,SN) passes the values for each field in structures S1,...,SN to function fun and returns the result in scalar structure S. Each field F in S, i.e. S.(F), is the result of fun(S1.(F),...,SN.(F)).

fieldfun allows the organization of data by structure and field name while allowing the data to be easily processed according to the semantically meaninful fields, improving code readability. When only one structure is supplied, fieldfun can also be used as an alternative to structfun, the difference being that the output of structfun is an array, while fieldfun returns a structure, retaining the input format, again improving readability.

Inputs

  • fun is either a function handle or a text scalar giving the function's name or operator. The function must take N input arguments, i.e. equal to the number of input structures, and return a value. fun is called once for each field F in S1,...,SN.
  • S1 is a scalar structure or array of structures.
  • S2,...,SN are scalar structures, arrays of structures, or variables of any other class. If S2,...,SN are structures, their fields and field order must match S1. If S2,...,SN are not structures, then they are converted to structures, with the value being assigned to each field.

Output

  • S is a scalar structure with the same fields and field order as S1. The value of each field F in S, i.e. S.(F), is the result of fun(S1.(F),...,SN.(F)).

Example

treatmentGroup(1) = struct( 'name', "John", 'age', 30, 'vaccinated', true );
treatmentGroup(2) = struct( 'name', "Jane", 'age', 80, 'vaccinated', false );
controlGroup = struct( 'name', "Jim", 'age', 50, 'vaccinated', true );
allParticipants = fieldfun( @horzcat, treatmentGroup, controlGroup )
allParticipants = struct with fields:
        name: ["John"    "Jane"    "Jim"]
         age: [30 80 50]
  vaccinated: [1 0 1]

More examples available in examples.mlx / examples.pdf.

Compatibility, License and Citation

Created in 2022b. Compatible with MATLAB release 2019b and later. Compatible with all platforms.

Published under MIT License (see LICENSE.txt).

Please cite George Abrahams (https://github.com/WD40andTape/fieldfun, https://www.linkedin.com/in/georgeabrahams).

引用格式

George Abrahams (2022) (https://www.linkedin.com/in/georgeabrahams). fieldfun: Apply function to matching fields of structures (https://github.com/WD40andTape/fieldfun/releases/tag/v1.0.0), GitHub. Retrieved December 29, 2022.

MATLAB 版本兼容性
创建方式 R2022b
与 R2019b 及更高版本兼容
平台兼容性
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
版本 已发布 发行说明
1.0.0

要查看或报告此来自 GitHub 的附加功能中的问题,请访问其 GitHub 仓库
要查看或报告此来自 GitHub 的附加功能中的问题,请访问其 GitHub 仓库