MATLAB Answers

1

How do I associate a custom icon with an EXE compiled with the MATLAB Compiler 4.0 (R14)?

I would like to include an icon file in the standalone application I have created with MATLAB Compiler. I want this icon to be displayed in Windows explorer instead of the generic application icon.

4 Answers

Answer by MathWorks Support Team on 15 Nov 2017
 Accepted Answer

The ability to add custom icons natively in a compiled application is available in the deploytool with release R2013b.
This is not possible in releases R2010b - R2013a.
If you are using MATLAB compiler 4.13 (R2010a) or before see below for any workarounds:
There are two ways to include a custom icon in a compiled application. One way is to modify the icon after the EXE has been generated using 3rd party software. The other way is to include the icon at compile time using a resource file. Each of these is explained further below.
1. In order to associate an icon with an application after the EXE has been created, you will need to use a 3rd party tool to modify the icon. There are many applications available for this purpose. A good place to start is by searching the "Downloads" area of some popular sites like http://www.pcworld.com/ and http://www.cnet.com/.
2. If you wish to include the icon at compile time, you will need to make sure that your compiler supports this. The general process is to create a resource file which points to the icon, compile it with a resource compiler, and then include the compiled resource file when compiling the application.
Steps for MSVC Compiler:
1. Create a new Win32 Console Application
2. Right-click on "Resource Files" then select "Add->New Item"
3. In the new window, under "Visual C++", select "Resource"
4. Then in the "Visual Studio installed templates" section, select "Resource File (.rc)"
5. Specify a name for the resource file and click on "Add"
6. Right-click on "Resource Files" again and choose "Add Resource"
7. Select "Icon" and click "New"
8. Create the icon
9. Build the solution (F7)
10. Copy the generated .res file to the folder with the MATLAB code
11. Compile the MATLAB code:
mcc -m myfile.m -M icon.res
where icon.res is the compiled resource file. You can use the "-M" flag to specify compile-time options to your specific compiler. Type
doc mcc
for more information about this flag. These steps may vary depending on which version of Microsoft Visual Studio you are using. If you are not using a Microsoft compiler, you may want to see if your 3rd party compiler supports icon files.
Steps for LCC Compiler:
1. Let the icon file be named 'ship.ico'. If you have an image file (*.JPEG, *.TIFF, *.GIF, *.PNG...), you will need to use a 3rd party tool (you can search online for an image converter) to convert your image to a .ICO icon file.
2. Create a new text file and write "ConApp ICON ship.ico" in it. Rename it as "example.rc" and save it in your current directory.
3. Compile "example.rc" and "ship.ico" files using the following command at MATLAB prompt:
system(['"' matlabroot '\sys\lcc\bin\lrc" /i "' pwd '\example.rc"']);
After this step you will have a resource file "example.res" created in your working directory.
4. Compile the MATLAB files and the resource file together using the -M option as before:
mcc -m file1.m -M example.res
where file1.m can be any MATLAB File you want to deploy. This will create a "file1.exe" in your working directory with the same icon as "ship.ico".
If you are using MATLAB compiler 4.14 (R2010b) then see below:
Steps for MSVC Compiler:
Here instead of creating a '.exe' file, you will need to create a C shared library and write wrapper driver code that calls the main function of the application. More information about this can be found in the link below.
<http://www.mathworks.com/help/releases/R2010a/toolbox/compiler/f13-1001757.html http://www.mathworks.com/help/releases/R2010a/toolbox/compiler/f13-1001757.html
>
A custom icon can then be associated with the EXE that is created when the driver code is compiled. For the sample driver code we are using FIGURETEST as the MATLAB function compiled into LIBFIGURE.DLL. Please follow the steps below once your DLL is created:
1. Create the C shared library using the link above.
2. Create a new Win32 Console Application named "test".
3. Right-click on "Resource Files" then select "Add->New Item"
4. In the new window, under "Visual C++", select "Resource"
5. Then in the "Visual Studio installed templates" section, select "Resource File (.rc)"
6. Specify a name for the resource file and click on "Add"
7. Right-click on "Resource Files" again and choose "Add Resource"
8. Select "Icon" and click "New"
9. Create the icon
10. Build the solution (F7)
11. Write your driver code in test.cpp and compile the VC++ console application by correctly providing the path to your library. Sample driver code is as follows.
// test.cpp : Defines the entry point for the console application.
// libfigure is the Shared Library
// Figuretest() is the main function in the library which corresponds to the
// figuretest() function in the .m file which was compiled using mcc
#include "stdafx.h"
#include <libfigure.h>
int _tmain(int argc, _TCHAR* argv[])
{
// Have to find out the paths
if( !mclInitializeApplication(NULL,0) )
{
fprintf(stderr, "Could not initialize the application.\n");
return -1;
}
// Initialize the library
if (!libfigureInitialize()){
fprintf(stderr,"Could not initialize the library.\n");
return -2;
}
else
{
//Call the figure library
mlfFiguretest();
mclWaitForFiguresToDie(NULL);
printf("Success");
// Terminate
libfigureTerminate();
mclTerminateApplication();
}
return 0;
}
Steps for LCC Compiler:
There are no workarounds other than using third party products to insert the icon after compiling the application.

  2 Comments

shall I get a '3rd party tool which can modify the icon'?
just now i search it but not get one, so could you send one to my email: delbet@qq.com. Thank you!
I followed your recommendation and used a third party tool (Resource Hacker, Windows 8.1) and tried to replace the Matlab icon in the deployed .exe file. The tool found an icon group containing 8 Matlab icons of different appearance, 16x16, 32x32, 48x48 and 256x256 at both 16 and 256 colors. I replaced each one of them with my own icon matching both size and color depth and saved the new .exe file. However, when I look at the .exe file in File manager, my logo only appears when I set the view to medium sized icons. All other views still show the original matlab icon. Any idea why?

Sign in to comment.


Answer by Qiang Gao on 14 Jul 2015

I followed the instructions, however, at the last step, when I use " mcc -m file1.m -M example.res", matlab displays "The option -M is ignored in standalone target mode (specify -? for help). " Does any know how to fix it?

  9 Comments

Henrik Enquist - If the workaround provided above does not work for you, please contact MathWorks Technical Support.
I have built a MATLAB standalone program. The original MATLAB program is supposed to import data from an excel file named P2.xlsx. After converting it to Standalone, if in change values in P2.xlsx, the output does not change.
How can I make it work?
Hi Seum Bin Rahman, it sounds like your coming into a different issue. Reach out to MathWorks Technical Support for assistance.

Sign in to comment.


Answer by Michael Latour on 3 May 2016

I think the easiest solution is to replace the default icons in toolbox->compiler->resources and then compile normally. Worked for me.

  2 Comments

This is interesting. Which files do you have to replace in which format and size?
In the toolbox->compiler->resources directory there are about a dozen icons labeled default_icon_*.png. Each file has a unique size but same icon. You can replace all of these .png files with others of the same size and format (you may want to backup the originals if you want to switch back later).

Sign in to comment.


Answer by Oscar Lundberg on 4 Jan 2018

I find it more convenient to write a short script to compile an application, rather than using the GUI to compile standalone .exe. How to I add icons then? I tried the first answer with Visual Studio, but it did not work and then I found out that the guide does not work for Matlab 2017a.

  0 Comments

Sign in to comment.