Skip to content
This repository was archived by the owner on Nov 25, 2018. It is now read-only.

Commit 8adb419

Browse files
committed
Add verbose errors, input file existence check
1 parent 177b360 commit 8adb419

File tree

1 file changed

+59
-4
lines changed

1 file changed

+59
-4
lines changed

mlapp2classdef.m

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,17 @@ function mlapp2classdef(pathToMLapp)
2929
% No input selected, prompt user to select a MATLAB app to process
3030
% Currently limited to single file selection
3131
[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
3337
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'});
3643
if iscell(pathToMLapp)
3744
[pathname, appname, ext] = cellfun(@fileparts, pathToMLapp, 'UniformOutput', false);
3845
else
@@ -43,11 +50,12 @@ function mlapp2classdef(pathToMLapp)
4350

4451
if iscell(pathToMLapp)
4552
for indF = 1:numel(pathToMLapp)
46-
% TODO: Check for existence of file
53+
checkfile(pathname{indF}, filename{indF}, ext{indF});
4754
processMlapp(pathname{indF}, filename{indF}, appname{indF});
4855
% TODO: Add a counter of successfully converted files.
4956
end
5057
else
58+
checkfile(pathname, filename, ext);
5159
processMlapp(pathname, filename, appname);
5260
end
5361

@@ -97,6 +105,53 @@ function processMlapp(pathname, filename, appname)
97105
disp(['Successfully unpacked ' filename '!']);
98106
end
99107

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\nExpected: 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+
100155
function nlines = countlines(filepath)
101156
% Count the number of lines present in the specified file.
102157
% filepath should be an absolute path

0 commit comments

Comments
 (0)