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

Commit f986a72

Browse files
committed
Add classdef parsing for newer MATLAB versions
MATLAB's type function can natively read and display a *.mlapp's class definition. This is a documented addition to R2016a, but has been tested successfully in R2014b and newer. This allows us to skip the intermediate zip file extraction and XML parsing. Though type does not support an output argument, the output can be piped to a character array using evalc and exported to an *.m file. Resolves: #8
1 parent 52f5e5d commit f986a72

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

mlapp2classdef.m

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,21 @@ function checkfile(pathname, filename, ext)
145145

146146

147147
function processapp(pathname, filename, appname, uielementflag)
148-
tmpdir = unpackapp(pathname, filename, appname);
149-
rawXML = loadXML(tmpdir);
150-
mymcode = stripXML(rawXML);
148+
isolderthanR2014b = verLessThan('matlab', '8.4');
149+
if isolderthanR2014b
150+
% *.mlapp is a *.zip file, extract the contents and strip out the XML
151+
% from the classdef
152+
tmpdir = unpackapp(pathname, filename, appname);
153+
rawXML = loadXML(tmpdir);
154+
mymcode = stripXML(rawXML);
155+
rmdir(tmpdir, 's');
156+
else
157+
% Beginning with R2014b MATLAB's type function supports *.mlapp files,
158+
% so we can pipe the output to an external file rather then extract
159+
% from the *.zip file
160+
evalcstr = sprintf('type(''%s'')', fullfile(pathname, filename));
161+
mymcode = evalc(evalcstr);
162+
end
151163

152164
if uielementflag.ReplaceAppUI
153165
% Convert App Designer UI elements to "regular" MATLAB UI elements
@@ -163,8 +175,7 @@ function processapp(pathname, filename, appname, uielementflag)
163175
end
164176
end
165177

166-
writemfile(mymcode, pathname, appname);
167-
rmdir(tmpdir, 's');
178+
writemfile(mymcode, pathname, appname, isolderthanR2014b);
168179
disp(['Successfully converted ' filename '!']);
169180
end
170181

@@ -211,12 +222,26 @@ function processapp(pathname, filename, appname, uielementflag)
211222
end
212223

213224

214-
function writemfile(mymcode, pathname, appname)
215-
% Write a cell array of strings to a *.m file.
216-
% Assumes each cell is a separate line.
225+
function writemfile(mymcode, pathname, appname, oldversionflag)
226+
% Write our m code to a file. Based on the MATLAB version used this will
227+
% either be in a cell array of strings (<R2014b) or a character array
228+
% (>=R2014b)
217229
fID = fopen(fullfile(pathname, sprintf('%s.m', appname)), 'w');
218-
for ii = 1:length(mymcode)
219-
fprintf(fID, '%s\n', mymcode{ii});
230+
if oldversionflag
231+
% MATLAB versions older than R2014b
232+
% Write a cell array of strings to a *.m file
233+
% Assumes each cell is a separate line
234+
for ii = 1:length(mymcode)
235+
fprintf(fID, '%s\n', mymcode{ii});
236+
end
237+
else
238+
% MATLAB versions R2014b and newer
239+
% Write a character array to a *.m file
240+
% Test for and strip out any leading whitespace characters
241+
if isspace(mymcode(1))
242+
mymcode(1) = [];
243+
end
244+
fprintf(fID, '%s', mymcode);
220245
end
221246
fclose(fID);
222247
end

0 commit comments

Comments
 (0)