From 4e65012e2d8eb48ef9472e3fdd9541dc3edcff2f Mon Sep 17 00:00:00 2001
From: fangq <fangq@786e58fb-9377-0410-9ff7-e4ac0ac0635c>
Date: Thu, 22 Dec 2011 14:18:44 +0000
Subject: [PATCH] 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
---
 mergestruct.m     | 33 +++++++++++++++++++++++++++++++++
 savejson.m        |  3 ++-
 varargin2struct.m | 40 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 75 insertions(+), 1 deletion(-)
 create mode 100644 mergestruct.m
 create mode 100644 varargin2struct.m

diff --git a/mergestruct.m b/mergestruct.m
new file mode 100644
index 0000000..ce344ad
--- /dev/null
+++ b/mergestruct.m
@@ -0,0 +1,33 @@
+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
+
diff --git a/savejson.m b/savejson.m
index bc10dab..217b747 100644
--- a/savejson.m
+++ b/savejson.m
@@ -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
diff --git a/varargin2struct.m b/varargin2struct.m
new file mode 100644
index 0000000..1dce180
--- /dev/null
+++ b/varargin2struct.m
@@ -0,0 +1,40 @@
+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
+
-- 
GitLab