Skip to content
Snippets Groups Projects
Commit 4e65012e authored by fangq's avatar fangq
Browse files

accept param,value pairs in savejson

git-svn-id: http://svn.code.sf.net/p/iso2mesh/code/trunk/jsonlab@333 786e58fb-9377-0410-9ff7-e4ac0ac0635c
parent 3358f292
No related branches found
No related tags found
No related merge requests found
function s=mergestruct(s1,s2)
%
% s=mergestruct(s1,s2)
%
% merge two struct objects into one
%
% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)
% date: 2012/12/22
%
% input:
% s1,s2: a struct object, s1 and s2 can not be arrays
%
% output:
% s: the merged struct object. fields in s1 and s2 will be combined in s.
%
% license:
% BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details
%
% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
%
if(~isstruct(s1) || ~isstruct(s2))
error('input parameters contain non-struct');
end
if(length(s1)>1 || length(s2)>1)
error('can not merge struct arrays');
end
fn=fieldnames(s2);
s=s1;
for i=1:length(fn)
s=setfield(s,fn{i},getfield(s2,fn{i}));
end
......@@ -57,7 +57,8 @@ if((isnumeric(obj) || islogical(obj) || ischar(obj)) && isempty(rootname))
rootlevel=0;
varname='';
end
json=obj2json(varname,obj,rootlevel,varargin{:});
opt=varargin2struct(varargin{:});
json=obj2json(varname,obj,rootlevel,opt);
if(rootisarray)
json=sprintf('%s\n',json);
else
......
function opt=vargin2struct(varargin)
%
% opt=vargin2struct('param1',value1,'param2',value2,...)
% or
% opt=vargin2struct(...,optstruct,...)
%
% convert a series of input parameters into a structure
%
% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)
% date: 2012/12/22
%
% input:
% 'param', value: the input parameters should be pairs of a string and a value
% optstruct: if a parameter is a struct, the fields will be merged to the output struct
%
% output:
% opt: a struct where opt.param1=value1, opt.param2=value2 ...
%
% license:
% BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details
%
% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
%
len=length(varargin);
opt=struct;
if(len==0) return; end
i=1;
while(i<=len)
if(isstruct(varargin{i}))
opt=mergestruct(opt,varargin{i});
elseif(ischar(varargin{i}) && i<len)
opt=setfield(opt,varargin{i},varargin{i+1});
i=i+1;
else
error('input must be in the form of ...,''name'',value,... pairs or structs');
end
i=i+1;
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment