Initializing a struct is helpful, both for allocation and to ensure the
order of fields in a structure is well defined (programmer preference).
When a structure has a lot of fields and they're all empty, its clunky to
write s = struct('a',[],'b',[],'c',[], ... ). fields2struct provides a
quick way of initializing a struct with fields such as a,b,c... with a
empty or user defined initial values
Usage:
s = fields2struct('field1','field2',...,'fieldN') creates a struct with N fields
- By default, the initial values are empty [] double arrays. Fieldnames
must be valid MATLAB variable names. Any invalid fieldnames will be
made valid with genvarname.
s = fields2struct(C) Fieldnames and order are derived cell array C
s = fields2struct(val,___) val is used as the default value.
Instead of [], the default value of all fields will populate with val
- In order to set the default value as a char array, use val = {'str'},
(set it inside of a 1x1 cell) as the first argument
- To define all the fields of the struct, set val 1xN cell array
containing the field values. Of course, this is a redundant way to
initialize a struct, but features are always good.
- Otherwise, if val is an NxM cell, the resulting structure will be NxM
with s(i,j).(field) = val(i,j) for all fields and i=1:n, j=1:m. This
generalized to n-dimensions
引用
Hank (2025). fields2struct (https://www.mathworks.com/matlabcentral/fileexchange/74948-fields2struct), MATLAB Central File Exchange. に取得済み.
MATLAB リリースの互換性
プラットフォームの互換性
Windows macOS Linuxタグ
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!