Home > vbmeg > external > iso2mesh > struct2jdata.m

struct2jdata

PURPOSE ^

SYNOPSIS ^

function newdata=struct2jdata(data,varargin)

DESCRIPTION ^

 newdata=struct2jdata(data,opt,...)

 convert a JData object (in the form of a struct array) into an array

 authors:Qianqian Fang (q.fang <at> neu.edu)

 input:
      data: a struct array. If data contains JData keywords in the first
            level children, these fields are parsed and regrouped into a
            data object (arrays, trees, graphs etc) based on JData 
            specification. The JData keywords are
               "_ArrayType_", "_ArraySize_", "_ArrayData_"
               "_ArrayIsSparse_", "_ArrayIsComplex_"
      opt: (optional) a list of 'Param',value pairs for additional options 
           The supported options include
               'Recursive', if set to 1, will apply the conversion to 
                            every child; 0 to disable

 output:
      newdata: the covnerted data if the input data does contain a JData 
               structure; otherwise, the same as the input.

 examples:
      obj=struct('_ArrayType_','double','_ArraySize_',[2 3],
                 '_ArrayIsSparse_',1 ,'_ArrayData_',null);
      ubjdata=struct2jdata(obj);

 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)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function newdata=struct2jdata(data,varargin)
0002 %
0003 % newdata=struct2jdata(data,opt,...)
0004 %
0005 % convert a JData object (in the form of a struct array) into an array
0006 %
0007 % authors:Qianqian Fang (q.fang <at> neu.edu)
0008 %
0009 % input:
0010 %      data: a struct array. If data contains JData keywords in the first
0011 %            level children, these fields are parsed and regrouped into a
0012 %            data object (arrays, trees, graphs etc) based on JData
0013 %            specification. The JData keywords are
0014 %               "_ArrayType_", "_ArraySize_", "_ArrayData_"
0015 %               "_ArrayIsSparse_", "_ArrayIsComplex_"
0016 %      opt: (optional) a list of 'Param',value pairs for additional options
0017 %           The supported options include
0018 %               'Recursive', if set to 1, will apply the conversion to
0019 %                            every child; 0 to disable
0020 %
0021 % output:
0022 %      newdata: the covnerted data if the input data does contain a JData
0023 %               structure; otherwise, the same as the input.
0024 %
0025 % examples:
0026 %      obj=struct('_ArrayType_','double','_ArraySize_',[2 3],
0027 %                 '_ArrayIsSparse_',1 ,'_ArrayData_',null);
0028 %      ubjdata=struct2jdata(obj);
0029 %
0030 % license:
0031 %     BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details
0032 %
0033 % -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
0034 %
0035 
0036 fn=fieldnames(data);
0037 newdata=data;
0038 len=length(data);
0039 if(jsonopt('Recursive',0,varargin{:})==1)
0040   for i=1:length(fn) % depth-first
0041     for j=1:len
0042         if(isstruct(getfield(data(j),fn{i})))
0043             newdata(j)=setfield(newdata(j),fn{i},jstruct2array(getfield(data(j),fn{i})));
0044         end
0045     end
0046   end
0047 end
0048 if(~isempty(strmatch('x0x5F_ArrayType_',fn)) && ~isempty(strmatch('x0x5F_ArrayData_',fn)))
0049   newdata=cell(len,1);
0050   for j=1:len
0051     ndata=cast(data(j).x0x5F_ArrayData_,data(j).x0x5F_ArrayType_);
0052     iscpx=0;
0053     if(~isempty(strmatch('x0x5F_ArrayIsComplex_',fn)))
0054         if(data(j).x0x5F_ArrayIsComplex_)
0055            iscpx=1;
0056         end
0057     end
0058     if(~isempty(strmatch('x0x5F_ArrayIsSparse_',fn)))
0059         if(data(j).x0x5F_ArrayIsSparse_)
0060             if(~isempty(strmatch('x0x5F_ArraySize_',fn)))
0061                 dim=double(data(j).x0x5F_ArraySize_);
0062                 if(iscpx && size(ndata,2)==4-any(dim==1))
0063                     ndata(:,end-1)=complex(ndata(:,end-1),ndata(:,end));
0064                 end
0065                 if isempty(ndata)
0066                     % All-zeros sparse
0067                     ndata=sparse(dim(1),prod(dim(2:end)));
0068                 elseif dim(1)==1
0069                     % Sparse row vector
0070                     ndata=sparse(1,ndata(:,1),ndata(:,2),dim(1),prod(dim(2:end)));
0071                 elseif dim(2)==1
0072                     % Sparse column vector
0073                     ndata=sparse(ndata(:,1),1,ndata(:,2),dim(1),prod(dim(2:end)));
0074                 else
0075                     % Generic sparse array.
0076                     ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3),dim(1),prod(dim(2:end)));
0077                 end
0078             else
0079                 if(iscpx && size(ndata,2)==4)
0080                     ndata(:,3)=complex(ndata(:,3),ndata(:,4));
0081                 end
0082                 ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3));
0083             end
0084         end
0085     elseif(~isempty(strmatch('x0x5F_ArraySize_',fn)))
0086         if(iscpx && size(ndata,2)==2)
0087              ndata=complex(ndata(:,1),ndata(:,2));
0088         end
0089         ndata=reshape(ndata(:),data(j).x0x5F_ArraySize_);
0090     end
0091     newdata{j}=ndata;
0092   end
0093   if(len==1)
0094       newdata=newdata{1};
0095   end
0096 end

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005