comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 % STRUCT2OBJ converts a structure to the wanted object.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: STRUCT2OBJ converts a structure to the wanted object.
5 %
6 % CALL: objs = struct2obj(struct, class_name)
7 % objs = struct2obj(struct)
8 %
9 % If the class type is not specified, then the fieldnames of the input
10 % structure are compared to the fieldnames of all LTPDA classes until a
11 % matching class is found.
12 %
13 % INPUTS: struct: Structure which should be converted
14 % class_name: Class name
15 %
16 % OUTPUTS: objs: Vector of the wanted objects.
17 %
18 % VERSION: $Id: struct2obj.m,v 1.4 2008/10/16 15:45:39 ingo Exp $
19 %
20 % HISTORY: 09-05-07 M Hewitson
21 % Creation
22 %
23 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
24
25 function varargout = struct2obj(varargin)
26
27 if nargin < 1
28 error('### Unknown number of inputs.%s### Please use: struct2obj(struct, class_name)', char(10))
29 end
30
31 obj_struct = varargin{1};
32 if nargin >= 2
33 class_name = varargin{2};
34 else
35 class_name = utils.helper.classFromStruct(obj_struct);
36 end
37
38 if isempty(class_name)
39 error('### The input structure doesn''t match any LTPDA class.');
40 end
41
42 obj_vec = [];
43
44 for jj = 1:numel(obj_struct)
45 if isstruct(obj_struct(jj))
46 if strcmp(class_name, 'sym')
47 obj = sym(obj_struct(jj).s);
48 else
49 obj = feval(class_name, obj_struct(jj));
50 end
51 obj_vec = [obj_vec obj];
52 elseif isa(obj_struct(jj), class_name)
53 obj_vec = [obj_vec obj_struct(jj)];
54 else
55 error('### Unknown Object (%s) for a %s object.', class(hist_s(jj)));
56 end
57 end
58
59 obj_vec = reshape(obj_vec, size(obj_struct));
60
61 varargout{1} = obj_vec;
62