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

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

Related

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.

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.

Mex binaries not loaded

I am trying to use this software available at http://www.cmap.polytechnique.fr/~aspremon/ZIP/COVSEL.zip. They have a function spmlcdvec defined in a matlab file spmlcdvec.m. It further calls a function BoxQP for which they have provided a binary mex file called BoxQP.mexmac. However, when I run the function spmlcdvec it says
Undefined function 'BoxQP' for input arguments of type 'double'.
What should I do? Whats going wrong?
Run mexext in your command window. If it does not return mexmac (or mexw32 which is also present in the folder) then you you won't be able to run the BoxQP function. These are simply old files - I see that they're from 2006. If you're using any form of OS X these days you'll get mexmaci64, which is not compatible. However, it looks like they have helpfully included all of the source code so you'll be able to hopefully compile the binaries for your system. For details on compiling mex code see this.
Then, as #alrikai suggests, your BoxQP function needs to be on your path. Either make sure that you call your code from the same folder or run path from the command window and check if that directory has been properly added (if you did so). See also addpath.

Matlab and addpath

I have recently been working with Matlab. My question stems from my usage over a few months and is something that I can't seem to solve. I have an external SVM toolbox (OSU-SVM) that I would like to interface to with my project. I am able to get the entire system to work when I add the path of the toolbox manually (Right click -> Add to Path -> Selected Folders and Subfolders). What I would like to do is add the folder in a script. I've tried the "addpath" command but for some reason I can't get it to find the library relative to the m-file (script) that I run the command from. The following is an example of the code:
% Add OSU SVM system
addpath(genpath('./osu-svm/'));
The reason the I'd like to add the path using a relative folder to the M-file is that the code needs to run in a different environment that will not have the toolbox install. The code is also going to be executed in a different OS to the one I am developing on. That is, I am running a Windows Matlab to develop the code and need to run the finished system on a Linux machine. The process of running my files needs to be as painless as possible and shouldn't require much input from the user. That is why I'm specifically trying to avoid manual addition of the path.
On a side note a similar problem occurs when I wish to use "uigetfile" using a relative path. I believe there is something I am missing that will help me solve both of these simultaneously. Any help would be greatly appreciated.
Instead of './osu-svm/' alone use
fullfile('.','osu-svm')
The reason it does not work for you on windows is that you are using forward slash file separators. Full file will make a file string containing the correct file separator for each OS.
The genpath example in the matlab documentation also uses fullfile
http://www.mathworks.co.uk/help/techdoc/ref/genpath.html
Furthermore, the '.' is kinda unnecessary as it just means "relative to the current directory" and can be left out of the command. Perhaps you meant one directory up?
'..'
???

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

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.