www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/StructureArrayInputsExample.m

    %% Structure Array Inputs  
% Parse structure array inputs with the |StructExpand| property set to |true|
% (default) or |false|.   

% Copyright 2015 The MathWorks, Inc.


%% 
% Expand a structure array input into parameter name and value pairs using
% the default |true| value of the |StructExpand| property. 
s.input1 = 10;
s.input2 = 20;
default = 0;

p = inputParser;
addParameter(p,'input1',default)
addParameter(p,'input2',default)
parse(p,s)

p.Results  
%% 
% Explicitly specifying a parameter name and value pair overrides values
% in the structure. 
parse(p,s,'input2',300)
p.Results  
%% 
% Accept a structure array input as a single argument by setting the |StructExpand|
% property to |false|. 
s2.first = 1;
s2.random = rand(3,4,2);
s2.mytext = 'some text';

p = inputParser;
p.StructExpand = false;
addRequired(p,'structInput')
parse(p,s2)

results = p.Results
fieldList = fieldnames(p.Results.structInput)