stackoverflowmatlabchat / mlapp2classdef Goto Github PK
View Code? Open in Web Editor NEWConvert MATLAB App's XML file to *.m class definition
Convert MATLAB App's XML file to *.m class definition
This basically saves us the hassle of unzipping and stripping of the xml...
This functionality was probably added in MATLAB R2016a with the release of the App Designer, so regex-based code is probably still relevant when converting using older versions.
Many of the sample *.mlapp
files from MATLAB's site utilize the setAutoResize
function during GUI initialization. Though it is not documented in R2016a, it is likely a feature introduced with the App Designer as it does not exist in R2015b.
Per this SO question, the wc
approach used for Linux and Mac based systems can potentially be contaminated.
To quote:
I have been using MATLAB's
system
command to get the result of some linux commands, like in the following simple example:
[junk, result] = system('find ~/ -type f')
This works as expected, unless the user types into MATLAB's command window at the same time. Which during a long find command is not uncommon. If this happens then the user's input seems to get mixed up with the result of the
find
command (and then things break).
The current XML parsing does not intelligently preallocate the cell array to the size of the data:
A = {};
ii = 1;
while ~feof(fID)
A{ii} = fgetl(fID);
ii = ii + 1;
end
fclose(fID);
This approach, while functional, is not particularly memory efficient and can potentially be a significant bottleneck depending on the size of the XML file being parsed. A more intelligent routine should be used to determine the length of the file and preallocate the cell array accordingly so it is not growing in memory.
The current fopen
call for parsing the XML file assumes a consistent name & location:
~\matlab\document.xml
Though this is consistent across tested *.mlapp
examples this is not a robust approach and also does not account for the potential for multiple XML files in the published app.
Hey,
Please excuse me if this has a really easy solution but I'm farily new to matlab and coding in general.
So I've been using mlapp2classdef to convert a simple app I made in App Designer just to test if I can end up with a final standalone file that works. The sample is just an axes with a push button that plots cos(x) after it is pushed. After I run the conversion, mlapp2classdef([], 'ReplaceAppUI, true)
I get an error saying:
While setting property 'UIAxes' of class 'testclick': Value must be 'matlab.ui.control.UIAxes'. Error in testclick/createComponents (line 39) app.UIAxes = axes(app.UIFigure); Error in testclick (line 79) createComponents(app)
My file name is "testclick". Here is the code that the conversion generates from my sample.
classdef testclick < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
end
methods (Access = private)
% Button pushed function: Plot
function PlotPushed(app, event)
x=0:.01:100;
y=cos(x);
plot(app.UIAxes,x,y);
app.XEditField.Value=10;
app.YEditField.Value=100;
end
end
% App initialization and construction
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure
app.UIFigure = figure;
app.UIFigure.Position = [100 100 634 308];
app.UIFigure.Name = 'UI Figure';
setAutoResize(app, app.UIFigure, true)
% Create UIAxes
app.UIAxes = axes(app.UIFigure);
title(app.UIAxes, 'Title');
xlabel(app.UIAxes, 'X');
ylabel(app.UIAxes, 'Y');
app.UIAxes.Position = [259 80 300 185];
% Create Plot
app.Plot = uicontrol('Parent', app.UIFigure, 'push', 'Style', 'pushbutton');
app.Plot.ButtonPushedFcn = createCallbackFcn(app, @PlotPushed, true);
app.Plot.Position = [96 216 100 22];
app.Plot.Text = 'Plot';
% Create XEditFieldLabel
app.XEditFieldLabel = uicontrol('Parent', app.UIFigure, 'Style', 'text');
app.XEditFieldLabel.HorizontalAlignment = 'right';
app.XEditFieldLabel.Position = [56 165 25 15];
app.XEditFieldLabel.Text = 'X';
% Create XEditField
app.XEditField = uicontrol('Parent', app.UIFigure, 'numeric', 'Style', 'edit');
app.XEditField.Position = [96 161 100 22];
% Create YEditFieldLabel
app.YEditFieldLabel = uicontrol('Parent', app.UIFigure, 'Style', 'text');
app.YEditFieldLabel.HorizontalAlignment = 'right';
app.YEditFieldLabel.Position = [57 118 25 15];
app.YEditFieldLabel.Text = 'Y';
% Create YEditField
app.YEditField = uicontrol('Parent', app.UIFigure, 'numeric', 'Style', 'edit');
app.YEditField.Position = [97 114 100 22];
end
end
methods (Access = public)
% Construct app
function app = testclick()
% Create and configure components
createComponents(app)
% Register the app with App Designer
% Function call removed
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
And lastly here is the code converted just with mlapp2classdef([])
classdef testclick < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
UIAxes matlab.ui.control.UIAxes
Plot matlab.ui.control.Button
XEditFieldLabel matlab.ui.control.Label
XEditField matlab.ui.control.NumericEditField
YEditFieldLabel matlab.ui.control.Label
YEditField matlab.ui.control.NumericEditField
end
methods (Access = private)
% Button pushed function: Plot
function PlotPushed(app, event)
x=0:.01:100;
y=cos(x);
plot(app.UIAxes,x,y);
app.XEditField.Value=10;
app.YEditField.Value=100;
end
end
% App initialization and construction
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure
app.UIFigure = uifigure;
app.UIFigure.Position = [100 100 634 308];
app.UIFigure.Name = 'UI Figure';
setAutoResize(app, app.UIFigure, true)
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Title');
xlabel(app.UIAxes, 'X');
ylabel(app.UIAxes, 'Y');
app.UIAxes.Position = [259 80 300 185];
% Create Plot
app.Plot = uibutton(app.UIFigure, 'push');
app.Plot.ButtonPushedFcn = createCallbackFcn(app, @PlotPushed, true);
app.Plot.Position = [96 216 100 22];
app.Plot.Text = 'Plot';
% Create XEditFieldLabel
app.XEditFieldLabel = uilabel(app.UIFigure);
app.XEditFieldLabel.HorizontalAlignment = 'right';
app.XEditFieldLabel.Position = [56 165 25 15];
app.XEditFieldLabel.Text = 'X';
% Create XEditField
app.XEditField = uieditfield(app.UIFigure, 'numeric');
app.XEditField.Position = [96 161 100 22];
% Create YEditFieldLabel
app.YEditFieldLabel = uilabel(app.UIFigure);
app.YEditFieldLabel.HorizontalAlignment = 'right';
app.YEditFieldLabel.Position = [57 118 25 15];
app.YEditFieldLabel.Text = 'Y';
% Create YEditField
app.YEditField = uieditfield(app.UIFigure, 'numeric');
app.YEditField.Position = [97 114 100 22];
end
end
methods (Access = public)
% Construct app
function app = testclick()
% Create and configure components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
The second statement in the regexprep
statement strips out characters following any end
statement in the input file.
A = regexprep(A, '(^.*)(?=classdef)|(?<=end)(.*$)', '');
Though likely not generally an issue, this is a very greedy implementation and could potentially negatively affect the user's code.
It is no secret that designing a UI in App Designer is much more convenient (one might even say fun) than using GUIDE. However, this comes at the price of lower customizability among other things.
It would be very useful to be able to design a UI in App Designer and then export it using "regular" elements (uicontrol
).
This functionality can conceptually be achieved by mapping various App Designer Component names to their corresponding uicontrol
/figure
/axes
counterparts.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.