Original blog illustration by Benedicte Rossi
This post marks the one year existence of this blog. I started writing in the hope to help folks get started with Matlab and also help rise the general quality of Matlab codes. Traffic have risen steadily in the last year as we are touching 900 pages per day of regular traffic for the first time today. Traffic, so far, has seemed to be doubling every 3 months! Whether I meet my goals, I don’t know, but at least this blog seems useful! For today, I chose a subject that fits into that anniversary context as it deals with how to package your Matlab programs for others.
Although Matlab is often used to quickly prototype ideas, professional applications can be done with Matlab. You can, if you want, create high-quality interfaces that benefits from the great calculation engine of Matlab. I found little help on the Internet on that subject (except for Yair’s excellent blog) so I thought it was a good occasion to help filling in the gap. In this post I talk about how to distribute your compiled application to colleagues/clients/friends in a very professional fashion.
There are some occasions when you need to distribute a great piece of code to colleagues, clients or friends that do not have a Matlab license. It might also be the case that you want to use your code on another machine that does not have access to your Matlab license.
In all of these cases, this job is for the Matlab compiler. I won’t document how to use the compiler here (this will be for another post). Let’s assume you have already compiled your application into a nice executable that you wish to distribution to your colleague/client/friend. You are probably thinking : Job done.
Of course not…
The last missing step is to create an installer. A setup file that would make packaging/distributing your executable file a real professional adventure. Every single application I download now has this so why not yours? This will make your colleague/friend/client life so much easier. You would think (as I did) that Matlab would do this for you (especially given the high price of that Compiling toolbox). Well no, it doesn’t. I hope it will in some distant future but for now, we have to do it ourselves.
The deployment process is somehow different depending on whether you want to deploy on Windows, Mac or Linux. I am going to document how to do this on Windows as I don’t have a linux machine available to work this out. For the Mac, I just want to say that the deployment process is a little bit of a joke. You have to go edit some system files, just for your deployed application to start. I am not going to extend on this but, if you read this, Mathworks friends, PLEASE finish the job on the Mac platform, we will be eternally grateful. I might post something about how annoying this is at some point… Anyway, today’s topic is how to package deployed applications in a nice Windows installer!
Let’s assume you have a compiled application that runs nicely. The thing you need to do is package this file in an installer. I have been using InnoSetup to do this. It works great and it’s free.
I recommend that you first start InnoSetup and go through the Wizard filling in the required fields. Everything is quite intuitive. When you are asked to select the main executable file, give your deployed application file. You should end with something that looks like this :
; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!#define MyAppName “My Program”
#define MyAppVersion “1.5”
#define MyAppPublisher “My Company, Inc.”
#define MyAppURL “http://www.example.com/”
#define MyAppExeName “MyProg.exe”[Setup]
; NOTE: The value of AppId uniquely identifies this application.
; Do not use the same AppId value in installers for other applications.
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
AppId={{A8F505D1-4A46-464E-89A9-66F3B86C17AD}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={pf}\{#MyAppName}
DefaultGroupName={#MyAppName}
OutputBaseFilename=setup
Compression=lzma
SolidCompression=yes[Languages]
Name: “english”; MessagesFile: “compiler:Default.isl”[Tasks]
Name: “desktopicon”; Description: “{cm:CreateDesktopIcon}”; GroupDescription: “{cm:AdditionalIcons}”; Flags: unchecked[Files]
Source: “C:\Program Files (x86)\Inno Setup 5\Examples\MyProg.exe”; DestDir: “{app}”; Flags: ignoreversion
; NOTE: Don’t use “Flags: ignoreversion” on any shared system files[Icons]
Name: “{group}\{#MyAppName}”; Filename: “{app}\{#MyAppExeName}”
Name: “{commondesktop}\{#MyAppName}”; Filename: “{app}\{#MyAppExeName}”; Tasks: desktopicon[Run]
Filename: “{app}\{#MyAppExeName}”; Description: “{cm:LaunchProgram,{#StringChange(MyAppName, ‘&’, ‘&&’)}}”; Flags: nowait postinstall skipifsilent
where MyProg.exe is your compiled application. You should generate a unique ID using ‘Generate GUID’ in the tools menu.
In the end it will look like that when starting the installer :
From that I highly recommend that you makes a few edits to add your icon files or else. But MOST IMPORTANTLY, you should have some checkup code to make sure the Matlab Component Runtime is locally installed. Here is what I use :
#define MyAppName “MyApp”
#define MyAppVersion “1.0”
#define MyAppPublisher “My Company, Inc”
#define MyAppURL “http://www.mycompany.com/”
#define MyAppExeName “MyApp.exe”[Setup]
AppId={{XXXX-XXX-XXX}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={pf}\{#MyAppName}
DefaultGroupName={#MyAppName}
InfoAfterFile=ReleaseNotes.txt
OutputBaseFilename=Setup-{#MyAppName}-{#MyAppVersion}
Compression=lzma
SolidCompression=yes
SetupIconFile=MyApp.ico
ArchitecturesInstallIn64BitMode=x64
LicenseFile=license.txt[CustomMessages]
MCRMissing=Matlab Compiler Runtime R2012b is required. Restart setup once it is installed. Do you want to download it ?
[Code]
function IsMCRDetected: Boolean;
var
ErrorCode: Integer;
MatlabMcrPath : String;
begin
Result := True;
if not RegQueryStringValue(HKEY_LOCAL_MACHINE_64, ‘SOFTWARE\MathWorks\MATLAB Compiler Runtime\8.0’,
‘MATLABROOT’, MatlabMcrPath) then
begin
Result := False;
if MsgBox(ExpandConstant(‘{cm:MCRMissing}’), mbConfirmation, MB_YESNO) = IDYES then
ShellExec(”, ‘http://www.mathworks.com/supportfiles/MCR_Runtime/R2012b/MCR_R2012b_win64_installer.exe’,
”,”, SW_SHOWNORMAL, ewNoWait, ErrorCode);
end;
end;function InitializeSetup: Boolean;
begin
Result := IsMCRDetected;
end;[Languages]
Name: “english”; MessagesFile: “compiler:Default.isl”[Tasks]
Name: “desktopicon”; Description: “{cm:CreateDesktopIcon}”; GroupDescription: “{cm:AdditionalIcons}”; Flags: unchecked
Name: “quicklaunchicon”; Description: “{cm:CreateQuickLaunchIcon}”; GroupDescription: “{cm:AdditionalIcons}”; Flags: unchecked; OnlyBelowVersion: 0,6.1[Files]
Source: “MyApp.exe”; DestDir: “{app}”; Flags: ignoreversion
Source: “ReleaseNotes.txt”; DestDir: “{app}”; Flags: ignoreversion
Source: “MyApp.ico”; DestDir: “{app}”; Flags: ignoreversion[Icons]
Name: “{group}\{#MyAppName}”; Filename: “{app}\{#MyAppExeName}”; IconFilename: “{app}\MyApp.ico”
Name: “{group}\{cm:UninstallProgram,{#MyAppName}}”; Filename: “{uninstallexe}”
Name: “{commondesktop}\{#MyAppName}”; Filename: “{app}\{#MyAppExeName}”; Tasks: desktopicon; IconFilename: “{app}\MyApp.ico”
Name: “{userappdata}\Microsoft\Internet Explorer\Quick Launch\{#MyAppName}”; Filename: “{app}\{#MyAppExeName}”; Tasks: quicklaunchicon; IconFilename: “{app}\MyApp.ico”
Feel free to adjust this to your own needs. The code after [Code] asks the end user to download the MCR only if it is not present in the registry. Adjust the MCR version (here 8.0) to the one you used to deployed your code.
And you, what do you do to make your code looks more professional when you distribute it?
NOTA: Starting in Matlab 2013b, Matlab introduced a built-in installer integrated with their deploytool. This change arrived in the release immediately following this post even on macOS! I’m not sure whether this was already planned by the MathWorks team or whether posts like this one nudged them in that direction.
Either way, it’s a welcome addition that simplifies deployment for many users. For those interested in exploring how Matlab handles packaging, deployment, and executable generation across different versions, here’s a curated 사이트모음 that outlines key tools and tips for sharing your Matlab applications.