Rev | Line | |
---|
[37] | 1 | function s = makestruct(varargin)
|
---|
| 2 | %MAKESTRUCT Cell-friendly alternative to STRUCT.
|
---|
| 3 | % MAKESTRUCT('field1',value1,...) is similar to STRUCT but allows values to
|
---|
| 4 | % be specified directly, i.e. cell arrays do not need to be wrapped.
|
---|
| 5 | % If a field is specified more than once, the last value is taken.
|
---|
| 6 | % You can also use MAKESTRUCT(C) where C is a cell array of fields/values.
|
---|
| 7 | %
|
---|
| 8 | % MAKESTRUCT is very useful for parsing argument lists.
|
---|
| 9 | % Example:
|
---|
| 10 | % function f(varargin)
|
---|
| 11 | % args = makestruct(varargin);
|
---|
| 12 | % default_args = struct('width',4,'height',4);
|
---|
| 13 | % args = setfields(default_args,args);
|
---|
| 14 | %
|
---|
| 15 | % See also STRUCT.
|
---|
| 16 |
|
---|
| 17 | % Written by Tom Minka
|
---|
| 18 | % (c) Microsoft Corporation. All rights reserved.
|
---|
| 19 |
|
---|
| 20 | args = varargin;
|
---|
| 21 | if length(args) == 1 && iscell(args{1})
|
---|
| 22 | args = args{1};
|
---|
| 23 | end
|
---|
| 24 | s = struct;
|
---|
| 25 | for i = 1:2:length(args)
|
---|
| 26 | % this is much faster than 'setfield'
|
---|
| 27 | s.(args{i}) = args{i+1};
|
---|
| 28 | %s = setfield(s,args{i},args{i+1});
|
---|
| 29 | end
|
---|
Note: See
TracBrowser
for help on using the repository browser.