@@ -29,10 +29,17 @@ function mlapp2classdef(pathToMLapp)
29
29
% No input selected, prompt user to select a MATLAB app to process
30
30
% Currently limited to single file selection
31
31
[filename , pathname ] = uigetfile(' *.mlapp' , ' Select MATLAB App' );
32
- [~ , appname ] = fileparts(filename );
32
+ if ~filename
33
+ error(' mlapp2classdef:NoFileSelected' , ' No file selected, exiting...' );
34
+ else
35
+ [~ , appname , ext ] = fileparts(filename );
36
+ end
33
37
else
34
- % TODO: Refactor to more verbose error generation
35
- validateattributes(pathToMLapp , {' char' , ' cell' }, {' vector' });
38
+ % Wrap validateattributes for more verbose error handling
39
+ % validateattributes won't catch if the cell array contains
40
+ % non-strings, but the subsequent fileparts call will error if these
41
+ % are encountered
42
+ pathToMLapp = validateattributes_wrapped(pathToMLapp , {' char' , ' cell' }, {' vector' });
36
43
if iscell(pathToMLapp )
37
44
[pathname , appname , ext ] = cellfun(@fileparts , pathToMLapp , ' UniformOutput' , false );
38
45
else
@@ -43,11 +50,12 @@ function mlapp2classdef(pathToMLapp)
43
50
44
51
if iscell(pathToMLapp )
45
52
for indF = 1 : numel(pathToMLapp )
46
- % TODO: Check for existence of file
53
+ checkfile(pathname{ indF }, filename{ indF }, ext{ indF });
47
54
processMlapp(pathname{indF }, filename{indF }, appname{indF });
48
55
% TODO: Add a counter of successfully converted files.
49
56
end
50
57
else
58
+ checkfile(pathname , filename , ext );
51
59
processMlapp(pathname , filename , appname );
52
60
end
53
61
@@ -97,6 +105,53 @@ function processMlapp(pathname, filename, appname)
97
105
disp([' Successfully unpacked ' filename ' !' ]);
98
106
end
99
107
108
+
109
+ function A = validateattributes_wrapped(A , classes , attributes )
110
+ % Wrap validateattributes with try-catch block for more verbose error
111
+ % handling
112
+ try
113
+ validateattributes(A , classes , attributes )
114
+ catch err
115
+ switch err .identifier
116
+ case ' MATLAB:invalidType'
117
+ newerr.identifier = ' mlapp2classdef:InvalidInputType' ;
118
+ newerr.message = sprintf(' Invalid input type: %s\n Expected: char, cell' , class(A ));
119
+ newerr.cause = err .cause ;
120
+ newerr.stack = err .stack ;
121
+ error(newerr );
122
+ case ' MATLAB:expectedVector'
123
+ % Warn and reshape
124
+ sizestr = sprintf(' %u ,' , size(A ));
125
+ sizestr = sizestr(1 : end - 1 ); % Strip trailing comma
126
+ warning(' mlapp2classdef:InvalidInputShape' , ...
127
+ ' Input cell array must be a vector of cells. Size of input array is: [%s ]. Reshaping...' , ...
128
+ sizestr ...
129
+ );
130
+ A = reshape(A , 1 , []);
131
+ otherwise
132
+ rethrow err
133
+ end
134
+ end
135
+ end
136
+
137
+
138
+ function checkfile(pathname , filename , ext )
139
+ % Check for existence of file
140
+ if exist(fullfile(pathname , filename ), ' file' )
141
+ % Check for correct file type
142
+ if ~strcmp(ext , ' .mlapp' )
143
+ error(' mlapp2classdef:InvalidFileType' , ...
144
+ ' ''%s'' is not a *.mlapp file' , fullfile(pathname , filename ) ...
145
+ );
146
+ end
147
+ else
148
+ error(' mlapp2classdef:FileNotFound' , ...
149
+ ' ''%s'' does not exist' , fullfile(pathname , filename ) ...
150
+ );
151
+ end
152
+ end
153
+
154
+
100
155
function nlines = countlines(filepath )
101
156
% Count the number of lines present in the specified file.
102
157
% filepath should be an absolute path
0 commit comments