-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewClass.m
133 lines (117 loc) · 3.17 KB
/
newClass.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
function newClass(varargin)
% Creates a new file for editing a class.
%
% newClass(NEWCLASSNAME) opens the editor and pastes the content of a
% user-defined template into the file NEWCLASSNAME.m.
%
%
% References
% Based on file 'tedit' from Peter Bodin.
%
% See also
% newEnum, newTest
%
% ------
% Author: David Legland
% e-mail: [email protected]
% Created: 2011-07-26, using Matlab 7.9.0.529 (R2009b)
% Copyright 2011 INRA - Cepia Software Platform.
switch nargin
case 0
edit
warning('newClass:MissingArgument', ...
'newClass without argument is the same as edit');
return;
case 1
fname = varargin{:};
edit(fname);
otherwise
error('too many input arguments');
end
% Matlab interface changed with 7.12.0, so we need to switch
if verLessThan('matlab','7.12.0')
try
% Define the handle for the java commands:
edhandle = com.mathworks.mlservices.MLEditorServices;
% get editor active document
doc = edhandle.builtinGetActiveDocument;
% append template header
text = parse(fname);
edhandle.builtinAppendDocumentText(doc, text);
catch ex
rethrow(ex)
end
else
try
% get editor active document
editorObject = matlab.desktop.editor.getActive;
% append template header
text = parse(fname);
editorObject.appendText(text);
catch ex
rethrow(ex)
end
end
function out = parse(func)
template = { ...
'classdef $filename < handle'
'% One-line description here, please.'
'%'
'% Class $filename'
'%'
'% Example'
'% $filename'
'%'
'% See also'
'%'
''
'% ------'
'% Author: $author'
'% e-mail: $mail'
['% Created: $date, using Matlab ' version]
'% Copyright $year $company.'
''
''
'%% Properties'
'properties'
'end % end properties'
''
''
'%% Constructor'
'methods'
' function obj = $filename(varargin)'
' % Constructor for $filename class.'
''
' end'
''
'end % end constructors'
''
''
'%% Methods'
'methods'
'end % end methods'
''
'end % end classdef'
''};
repstr = {...
'$filename'
'$FILENAME'
'$date'
'$year'
'$author'
'$mail'
'$company'};
repwithstr = {...
func
upper(func)
datestr(now, 29)
datestr(now, 10)
'David Legland'
'INRAE - BIA-BIBS'};
for k = 1:numel(repstr)
template = strrep(template, repstr{k}, repwithstr{k});
end
out = sprintf('%s\n', template{:});
end
end