Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/Coursera-Machine-Learning
Path: blob/master/Week 9/Programming Assignment - 8/ex8/lib/jsonlab/varargin2struct.m
626 views
1
function opt=varargin2struct(varargin)
2
%
3
% opt=varargin2struct('param1',value1,'param2',value2,...)
4
% or
5
% opt=varargin2struct(...,optstruct,...)
6
%
7
% convert a series of input parameters into a structure
8
%
9
% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)
10
% date: 2012/12/22
11
%
12
% input:
13
% 'param', value: the input parameters should be pairs of a string and a value
14
% optstruct: if a parameter is a struct, the fields will be merged to the output struct
15
%
16
% output:
17
% opt: a struct where opt.param1=value1, opt.param2=value2 ...
18
%
19
% license:
20
% BSD, see LICENSE_BSD.txt files for details
21
%
22
% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
23
%
24
25
len=length(varargin);
26
opt=struct;
27
if(len==0) return; end
28
i=1;
29
while(i<=len)
30
if(isstruct(varargin{i}))
31
opt=mergestruct(opt,varargin{i});
32
elseif(ischar(varargin{i}) && i<len)
33
opt=setfield(opt,varargin{i},varargin{i+1});
34
i=i+1;
35
else
36
error('input must be in the form of ...,''name'',value,... pairs or structs');
37
end
38
i=i+1;
39
end
40
41
42