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

Commit

Permalink
Add classdef parsing for newer MATLAB versions
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sco1 committed May 3, 2016
1 parent 52f5e5d commit f986a72
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions mlapp2classdef.m
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,21 @@ function checkfile(pathname, filename, ext)


function processapp(pathname, filename, appname, uielementflag)
tmpdir = unpackapp(pathname, filename, appname);
rawXML = loadXML(tmpdir);
mymcode = stripXML(rawXML);
isolderthanR2014b = verLessThan('matlab', '8.4');
if isolderthanR2014b
% *.mlapp is a *.zip file, extract the contents and strip out the XML
% from the classdef
tmpdir = unpackapp(pathname, filename, appname);
rawXML = loadXML(tmpdir);
mymcode = stripXML(rawXML);
rmdir(tmpdir, 's');
else
% Beginning with R2014b MATLAB's type function supports *.mlapp files,
% so we can pipe the output to an external file rather then extract
% from the *.zip file
evalcstr = sprintf('type(''%s'')', fullfile(pathname, filename));
mymcode = evalc(evalcstr);
end

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

writemfile(mymcode, pathname, appname);
rmdir(tmpdir, 's');
writemfile(mymcode, pathname, appname, isolderthanR2014b);
disp(['Successfully converted ' filename '!']);
end

Expand Down Expand Up @@ -211,12 +222,26 @@ function processapp(pathname, filename, appname, uielementflag)
end


function writemfile(mymcode, pathname, appname)
% Write a cell array of strings to a *.m file.
% Assumes each cell is a separate line.
function writemfile(mymcode, pathname, appname, oldversionflag)
% Write our m code to a file. Based on the MATLAB version used this will
% either be in a cell array of strings (<R2014b) or a character array
% (>=R2014b)
fID = fopen(fullfile(pathname, sprintf('%s.m', appname)), 'w');
for ii = 1:length(mymcode)
fprintf(fID, '%s\n', mymcode{ii});
if oldversionflag
% MATLAB versions older than R2014b
% Write a cell array of strings to a *.m file
% Assumes each cell is a separate line
for ii = 1:length(mymcode)
fprintf(fID, '%s\n', mymcode{ii});
end
else
% MATLAB versions R2014b and newer
% Write a character array to a *.m file
% Test for and strip out any leading whitespace characters
if isspace(mymcode(1))
mymcode(1) = [];
end
fprintf(fID, '%s', mymcode);
end
fclose(fID);
end
Expand Down

0 comments on commit f986a72

Please sign in to comment.