29

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AP.
  • 750
  • 3
  • 12
  • 19

8 Answers8

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.

phoebus
  • 14,673
  • 2
  • 33
  • 35
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
Doresoom
  • 7,398
  • 14
  • 47
  • 61
7

Try:

mcc -m yourfile

Also see help mcc

Andreas Bonini
  • 44,018
  • 30
  • 122
  • 156
  • Thanks , it works great, but not sure if it works in non matlab installed machines. – AP. Dec 18 '09 at 20:02
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%.)

Jason S
  • 184,598
  • 164
  • 608
  • 970
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.

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.

Hamish Grubijan
  • 10,562
  • 23
  • 99
  • 147
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.

Francis
  • 11
  • 1
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.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
csafonov
  • 1
  • 3
  • and does it work for various aspects of MATLAB that depends upon the OS like the GUI APIs? – Vass Jun 28 '18 at 00:49