How do I use Matlab Compiler SDK to create executable for GUI app

22 views (last 30 days)
Hi. I made a simple GUI app using maltab app designer. After exported it as .m file, I'd like to compile it using compiler SDK to C++ share library then somehow generate a executable file. I was following this document: Generate a C++ MATLAB Data API Shared Library and Build a C++ Application. I've tried to work through the document on a non-GUI .m file and successfully got an functional exe. But when I tried to implement same steps for the GUI app, I had an issue. If I run the exe, the window only appeared for a moment, like half a second, and then closed/crushed automatically. Same thing happened for Matlab plot feature. Basically, anything that has a graphical interface, I couldn't hold the window.
My app:
.m code (exported from app designer):
classdef sine_ploter < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
ClearButton matlab.ui.control.Button
Slider matlab.ui.control.Slider
PlotButton matlab.ui.control.Button
UIAxes matlab.ui.control.UIAxes
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: PlotButton
function PlotButtonPushed(app, ~)
x=0:pi/200:2*pi;
v = app.Slider.Value;
y=sin(v * x);
plot(app.UIAxes,x,y);
end
% Button pushed function: ClearButton
function ClearButtonPushed(app, ~)
cla(app.UIAxes);
end
% Value changed function: Slider
function SliderValueChanged(~, ~)
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Color = [1 1 1];
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'MATLAB App';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Y=sin(X)')
xlabel(app.UIAxes, 'X')
ylabel(app.UIAxes, 'Y')
zlabel(app.UIAxes, 'Z')
app.UIAxes.XGrid = 'on';
app.UIAxes.YGrid = 'on';
app.UIAxes.GridColor = [0 0 1];
app.UIAxes.Position = [50 150 541 296];
% Create PlotButton
app.PlotButton = uibutton(app.UIFigure, 'push');
app.PlotButton.ButtonPushedFcn = createCallbackFcn(app, @PlotButtonPushed, true);
app.PlotButton.Position = [122 31 150 43];
app.PlotButton.Text = 'Plot';
% Create Slider
app.Slider = uislider(app.UIFigure);
app.Slider.Limits = [1 10];
app.Slider.ValueChangedFcn = createCallbackFcn(app, @SliderValueChanged, true);
app.Slider.Position = [233 132 175 3];
app.Slider.Value = 1;
% Create ClearButton
app.ClearButton = uibutton(app.UIFigure, 'push');
app.ClearButton.ButtonPushedFcn = createCallbackFcn(app, @ClearButtonPushed, true);
app.ClearButton.Position = [388 31 150 43];
app.ClearButton.Text = 'Clear';
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = sine_ploter
% Create UIFigure and 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
My Sample script:
% Sample script to demonstrate execution of function sine_ploter(app, ~)
sine_ploter();
Any suggestions? Please and Thank you!!

Answers (2)

Yash Sharma
Yash Sharma on 23 Nov 2023
I understand that you have a MATLAB GUI app and want to make a standalone executable of the same. Here are the steps that you can follow to convert your GUI app to standalone executable.
  1. Make sure you have MATLAB Compiler and supported C compiler.
  2. Run the command mbuild -setup to select your C compiler.
  3. Run the command “deploytool” to open the deployment tool window.
  4. Select Application compiler from the list of options.
  5. In the main file tab click on the “+” button and select your main file.
  6. Enter necessary information in application information window.
  7. Click on “package”.
Please find below links to documentation which will be helpful for your implementation:
Hope this helps!

Steven Lord
Steven Lord on 23 Nov 2023
I would try writing and deploying a function that opens the app then waits for the app to close before the function returns. Something like this I think would work (though I haven't tried it.)
function deployMyApp()
app = sine_ploter;
waitfor(app)
end

Categories

Find more on C Shared Library Integration in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!