Professional deployment of Matlab code

Original blog illustration by Cali Rossi

Original blog illustration by Cali 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!

InnoSetupLet’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 :Installer InnoSetup

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 now provides a built-in installer with their deploytool. This happened in the next release following this post (including on the MAC!). I don’t know whether this was in their pipeline or whether this post helped convinced this would be useful. In any cases, it’s a nice new addition!

Related Posts

This entry was posted in Advanced, Making interfaces. Bookmark the permalink.

8 Responses to Professional deployment of Matlab code

  1. Yair Altman says:

    Hi Jerome,

    Thanks for the nice mention :-)

    Checking for MCR existence in the installer is a nice touch.

    I typically wrap my deployed apps in a wrapper executable that displays a splash screen until the main GUI loads (http://undocumentedmatlab.com/blog/splash-window-for-deployed-applications/). I do this because the MCR takes such a loooooooong time to load, especially the first time after a boot, and without a splash screen users will see no feedback for up to a full minute (unacceptable). So in my case, I would place the splash wrapper exe in the installer, rather than the actual target Matlab exe. I’d also need to take care of the extra parameters to submit to the splash wrapper etc. – these things can usually be configured by the installer.

  2. Hi,
    I am new to pascal or the above innoSetup.I found this to be very useful. Thanks for your post.!!
    Is that possible to include the version of MCR installer (v7.16) as an .exe instead of making the user to download the required version from the weblink. I think some changes to be done at ShellExec and the [Files] but I unable to understand how to proceed. Can you please help me out… !

    Thanks for your time.

  3. James Kerns says:

    Nice intro on this subject! I’ve been wanting to develop my programs for professional distribution but always felt amateur-ish when i would hand over the matlab *.exe by itself. Although I know nothing about Inno Setup I would hope I could create subfolders and so forth to hold example files, help docs, outputs, etc. Have you noticed anything else of interest since publishing this? Thanks!

  4. Yair Churi says:

    Hi! Thank you for the post. I have developed a Matlab application on my Mac and now I want to deploy it to Windows users. I noticed that you have mentioned that its somewhat problematic (?) any pointers on how I can do this?

    • Jerome says:

      Hi Yair,
      You can’t do this on your mac. You can only deploy on the platform of your Matlab installation (as it needs to have access to all libraries that are compiled for your platform. So basically, you need to have a windows installation, either using VMWare or on a PC.

  5. Michael Anderson says:

    Excellent post, thanks.

    A cautionary note: beware of cutting and pasting the code samples above as I did. The quotation marks are different to those required by my version of Inno Setup. Change them to standard quotation marks first.

Leave a Reply