How to create an executable .exe file from a .m file
Asked Answered
B

8

30

I was wondering if there is a way to create a '.exe' file from ' .m' file in MATLAB, such that it can be run in machine which does not have MATLAB (like it can be done in C, C++).

I know writing a MATLAB function is one way, but I am not sure if it can run in machine without MATLAB.

Also I would like to hide my code and just create a script which can be run by a user using his own data files.

Bluebeard answered 18/12, 2009 at 3:26 Comment(0)
S
22

The Matlab Compiler is the standard way to do this. mcc is the command. The Matlab Runtime is required to run the programs; I'm not sure if it can be directly integrated with the executable or not.

Safelight answered 18/12, 2009 at 3:33 Comment(4)
One thing to note - this is a toolbox that you have to pay extra for.Jehius
Good point. I wasn't sure because I've always worked on University installs that included it.Safelight
The Matlab Compiler is pricey - it will set you back $5K.Brachio
...and deploytool provides an option to include the MCR library with your standalone executable.Brachio
B
9

If you have MATLAB Compiler installed, there's a GUI option for compiling. Try entering

deploytool

in the command line. Mathworks does a pretty good job documenting how to use it in this video tutorial: http://www.mathworks.com/products/demos/compiler/deploytool/index.html

Also, if you want to include user input such as choosing a file or directory, look into

uigetfile % or uigetdir if you need every file in a directory

for use in conjunction with

guide
Brachio answered 18/12, 2009 at 17:53 Comment(2)
does the GUI or figure production compile successfully ?Vyatka
@Vass, yes it will compile GUIs too.Brachio
S
8

Try:

mcc -m yourfile

Also see help mcc

Sagacity answered 18/12, 2009 at 3:32 Comment(1)
Thanks , it works great, but not sure if it works in non matlab installed machines.Bluebeard
G
2

If your code is more of a data analysis routine (vs. visualization / GUI), try GNU Octave. It's free and many of its functions are compatible with MATLAB. (Not 100% but maybe 99.5%.)

Gaskell answered 18/12, 2009 at 15:45 Comment(1)
99.32% of statistics are made up on the spotSagacity
K
2
mcc -?

explains that the syntax to make *.exe (Standalone Application) with *.m is:

 mcc -m <matlabFile.m> 

For example:

mcc -m file.m

will create file.exe in the curent directory.

Katlynkatmai answered 20/6, 2018 at 8:22 Comment(0)
C
1

It used to be possible to compile Matlab to C with older versions of Matlab. Check out other tools that Matlab comes with.

Newest Matlab code can be exported as a Java's jar or a .Net Dll, etc. You can then write an executable against that library - it will be obfuscated by the way. The users will have to install a freely available Matlab Runtime.

Like others mentioned, mcc / mcc.exe is what you want to convert matlab code to C code.

Chemoprophylaxis answered 18/12, 2009 at 3:33 Comment(0)
P
1

The "StandAlone" method to compile .m file (or files) requires a set of Matlab published library (.dll) files on a target (non-Matlab) platform to allow execution of the compiler generated .exe.

Check MATLAB main site for their compiler products and their limitations.

Piano answered 11/5, 2015 at 19:34 Comment(0)
G
0

I developed a non-matlab software for direct compilation of m-files (TMC Compiler). This is an open-source converter of m-files projects to C. The compiler produces the C code that may be linked with provided open-source run-time library to produce a stand-alone application. The library implements a set of build-in functions; the linear-algebra operations use LAPACK code. It is possible to expand the set of the build-in functions by custom implementation as described in the documentation.

Graceless answered 12/8, 2015 at 8:44 Comment(1)
and does it work for various aspects of MATLAB that depends upon the OS like the GUI APIs?Vyatka

© 2022 - 2024 — McMap. All rights reserved.