diff m-toolbox/classes/+utils/@helper/struct2obj.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/m-toolbox/classes/+utils/@helper/struct2obj.m	Wed Nov 23 19:22:13 2011 +0100
@@ -0,0 +1,62 @@
+% STRUCT2OBJ converts a structure to the wanted object.
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+% DESCRIPTION: STRUCT2OBJ converts a structure to the wanted object.
+%
+% CALL:        objs  = struct2obj(struct, class_name)
+%              objs  = struct2obj(struct)
+%
+% If the class type is not specified, then the fieldnames of the input
+% structure are compared to the fieldnames of all LTPDA classes until a
+% matching class is found.
+%
+% INPUTS:      struct:     Structure which should be converted
+%              class_name: Class name
+%
+% OUTPUTS:     objs:       Vector of the wanted objects.
+%
+% VERSION:     $Id: struct2obj.m,v 1.4 2008/10/16 15:45:39 ingo Exp $
+%
+% HISTORY:     09-05-07 M Hewitson
+%                 Creation
+%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+function varargout = struct2obj(varargin)
+
+if nargin < 1
+  error('### Unknown number of inputs.%s### Please use: struct2obj(struct, class_name)', char(10))
+end
+
+obj_struct = varargin{1};
+if nargin >= 2
+  class_name = varargin{2};
+else
+  class_name = utils.helper.classFromStruct(obj_struct);
+end
+
+if isempty(class_name)
+  error('### The input structure doesn''t match any LTPDA class.');
+end
+
+obj_vec = [];
+
+for jj = 1:numel(obj_struct)
+  if isstruct(obj_struct(jj))
+    if strcmp(class_name, 'sym')
+      obj = sym(obj_struct(jj).s);
+    else
+      obj = feval(class_name, obj_struct(jj));
+    end
+    obj_vec = [obj_vec obj];
+  elseif isa(obj_struct(jj), class_name)
+    obj_vec = [obj_vec obj_struct(jj)];
+  else
+    error('### Unknown Object (%s) for a %s object.', class(hist_s(jj)));
+  end
+end
+
+obj_vec = reshape(obj_vec, size(obj_struct));
+
+varargout{1} = obj_vec;
+