source: issm/trunk/src/m/kml/kml_object.m@ 6456

Last change on this file since 6456 was 6456, checked in by jschierm, 14 years ago

Probably should test before checking in...

File size: 4.5 KB
RevLine 
[6454]1%
2% definition for the kml_object super (base) class.
3%
4% [kml]=kml_object(varargin)
5%
6% where the optional varargin and defaults are:
7% id (char, object id, '')
8%
9% note that zero arguments constructs a default instance; one
10% argument of the class copies the instance; and two or more
11% arguments constructs a new instance from the arguments.
12%
13classdef kml_object
14 properties
15 id ='';
16 end
17
18 methods
19 function [kml]=kml_object(varargin)
20
21 switch nargin
22
23% create a default object
24
25 case 0
26
27% copy the object or create the object from the input
28
29 otherwise
30 if (nargin == 1) && isa(varargin{1},class(kml))
31 kml=varargin{1};
32
33 else
34 fnames=fieldnames(kml);
35
36 for i=1:min(nargin,length(fnames))
37 if isa(varargin{i},class(kml.(fnames{i})))
38 if ~isempty(varargin{i})
39 kml.(fnames{i})=varargin{i};
40 end
41 else
42 if ~isempty(inputname(i))
[6455]43 warning('Argument ''%s'' for property ''%s'' is a ''%s'' class object, not ''%s''.',...
[6454]44 inputname(i),fnames{i},class(varargin{i}),class(kml.(fnames{i})));
45 else
[6455]46 warning('Argument %d for property ''%s'' is a ''%s'' class object, not ''%s''.',...
[6454]47 i ,fnames{i},class(varargin{i}),class(kml.(fnames{i})));
48 end
49 end
50 end
51 end
52
53 end
54
55 end
56
57% display the object
58
59 function []=disp(kml)
60
61 for i=1:numel(kml)
62 if strcmp(class(kml),'kml_object')
63 disp(sprintf('class ''%s'' object ''%s%s'' = \n',...
64 class(kml),inputname(1),string_dim(kml,i)));
65 end
66
67 if strcmp(class(kml),'kml_object')
68 disp(sprintf(' id: ''%s''\n',kml(i).id));
69 else
70 disp(sprintf(' id: ''%s''' ,kml(i).id));
71 end
72 end
73
74 end
75
[6455]76% set the properties of the object
77
78 function [kml]=set(kml,varargin)
79
80 kmlref=feval(class(kml));
[6456]81 fnames=fieldnames(kmlref);
[6455]82
83% loop through each parameter in the input list (comparing to the reference
84% object in case property types have been changed)
85
86 for i=1:2:length(varargin)
[6456]87 if ismember(varargin{i},fnames) && (i+1 <= length(varargin))
[6455]88 if isa(varargin{i+1},class(kmlref.(varargin{i})))
89 kml.(varargin{i})=varargin{i+1};
90 else
91 if ~isempty(inputname(i+1))
92 warning('Argument ''%s'' for property ''%s'' is a ''%s'' class object, not ''%s''.',...
[6456]93 inputname(i+2),varargin{i},class(varargin{i+1}),class(kmlref.(varargin{i})));
[6455]94 else
95 warning('Argument %d for property ''%s'' is a ''%s'' class object, not ''%s''.',...
[6456]96 i+2 ,varargin{i},class(varargin{i+1}),class(kmlref.(varargin{i})));
[6455]97 end
98 end
99 else
100 warning('Property ''%s'' for class ''%s'' does not exist.',...
101 varargin{i},class(kmlref));
102 end
103 end
104
105 end
106
[6454]107% write the object
108
109 function []=kml_write(kml,fid,indent)
110
111 if ~exist('fid','var') || isempty(fid)
112 fid=1;
113 end
114 if ~exist('indent','var') || isempty(indent)
115 indent='';
116 end
117
118% loop over the objects
119
120 for i=1:numel(kml)
121 if strcmp(class(kml),'kml_object')
122 if ~isempty(kml(i).id)
123 fprintf(fid,'%s<!Object id="%s">\n',indent,kml(i).id);
124 else
125 fprintf(fid,'%s<!Object>\n',indent);
126 end
127 end
128
129 if strcmp(class(kml),'kml_object')
130 fprintf(fid,'%s</!Object>\n',indent);
131 end
132 end
133
134 end
135
136 end
137
138end
139
Note: See TracBrowser for help on using the repository browser.