How to create an executable .exe file from a .m file - matlab

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.

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.

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

Try:
mcc -m yourfile
Also see help mcc

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%.)

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.

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.

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.

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.

Related

Matlab Ode23 C++ file

I want to know can i use this code for my projects?
ftp://ftp.nodc.noaa.gov/nodc/archive/arc0032/0071189/1.1/data/0-data/AT003L11/RR_MOVED/FUNFUN/ODE23.C
it uses a include mex.h file in the beginning. what is that? what is that mex header doing there? can i just ignore that line and use the rest of the code in somewhere else?
I want to use the file in android NDK.
This file is meant to be compiled for Matlab to process. If you have Matlab installed you can compile it from the console. If not you will not be able to compile this by simply removing the include statement. Your best bet is to read through the code, understand what it is doing, and use it as an example for your own code.

How can i copy m-files to a specific folder in MATLAB compiled application?

I'm using MATLAB R2015a application compiler. After choosing my main m-file, MATLAB found some files required for application to run but after compiling and running my application, the compiled application can't find some specific m-files from an installed toolbox (third-party toolbox).
Now i want copy all toolbox files to target folder C:\Users\My_Account\AppData\Local\Temp\My_Account\mcrCache8.5\Program_70\DDM. DDM is my toolbox folder that MATLAB creating it automatically based on nested files as mentioned above. I want add other files to this folder. How can I do this? and is this a good solution to fix this problem?
What you're trying to do won't work.
When you compile an application with MATLAB Compiler, it finds all code that your main function depends on, encrypts it, and packages it into an executable that will later be executed against the MATLAB Compiler Runtime (MCR).
The MCR can only execute code that has been encrypted and packaged in this way - it is not possible to get it to execute a regular unencrypted MATLAB file. So dumping things into the temp folder that the MCR uses to unpackage code files will not achieve anything.
Instead try to figure out why, during the dependency analysis, MATLAB Compiler is not finding all the files that your main function depends on.
There are various reasons why that might happen - the dependency analysis is not perfect. For example, if your code calls eval('myfunction'), the dependency analysis will not find myfunction. I answered another question recently where another cause was the issue.
In these situations you can explicitly tell MATLAB Compiler that there is a dependency on myfunction, using the %#function pragma. Within the file that contains the hidden call to myfunction, at the top of the file (actually anywhere, but typically you'd put it at the top), put
%#function myfunction
MATLAB Compiler will then force that to be a dependency and include it in the packaging.
Alternatively, when setting up the packaging with mcc or in the deploytool app, you can just manually add myfunction to the package yourself (although this will then not find things that myfunction depends on).

Matlab Using .p file for compiling?

I m working on a Matlab project and I need UsbWebcams package for capture image from webcam. I can run .m file in matlab but when I compile project to create an exe file, My exe file return an error because usbWebcams package have some special .p files(Utility.p,webcamchannel.p etc) and I can not use these file for compiling.I searced on Internet and I didnt find any answer for this. How can I use .p files in my project. I think there should be a solution and I should find it. Thanks for helping to all.
Although MATLAB Compiler should be able to compile .p files, it's possible that the .p files you're trying to compile may have dependencies that you can't see because they're p-coded. For example, they might call an external library (this is quite possible if they are for interfacing with a webcam), or they might call another function using eval.
Whether they're .m files or .p files, if the files you're trying to compile have a dependency of this sort you need to include it explicitly for the Compiler, otherwise it won't know where to find it. But if the file is p-coded, it's tough to find out what the dependencies might be. You might need to ask MathWorks directly for support in compiling this functionality.

How to create mex file in matlab having just object file, without source?

I have a precompiled static library propa64.a, which i want to use in Matlab in Linux.
No source code of it available, only header file (propa.h). As far as i understand, to use it in Matlab i need to create mex file. How can i do it, without source and with only compiled propa64.a?
mex propa64.a
doesn't work, matlab says "mex: no filename given" error.
Any suggestions?
Or , may be it's possible to use pre-compiled static library in Matlab somehow else?

Is there anyway to make an executable compatible by using matlab mex?

The executable is created by using mex. Currently I tried it on windows, but eventually I will need it for windows, linux, and mac os x.
The problem is it can only be used with the same version of matlab which I used to create it.
The main reason I think is if different version of matlab is used, then the path for libraries used by the executable will be different.
Is there any way that I can create an executable from mex which can be compatible with different versions of matlab?
Is possible that I set paths for libraries and header files associated with multiple versions of matlab?
Please correct me if I understand anything wrong above. Thanks a lot.
More detail information:
The way to generate my executable is to call:
mex('-v', '-f', [matlabroot '\bin\win32\mexopts\msvc90engmatopts.bat'], 'myexecutable.cpp');
In the msvc90engmatopts.bat file, paths are set for librarires and header files and etc.
msvc90engmatopts.bat is from matlab.