How to run a Matlab function as a standalone executable

If you have a function, you can compile and run it without Matlab.
To do that, just write a .m file like test_cpu.m and leave it as follows:

function [time]= test_cpu(n)
if isdeployed
     n=str2double(n);
end
A = rand(n,n);
tic;
fft(A);
time = toc;
disp(time);

It's important check is the file is deployed (compiled) to convert the argument to double for avoid this error:

Error using rand
CLASSNAME input must be a class that supports RAND, for example, 'single' or 'double'.

Error in test_cpu (line 2)


MATLAB:rand:invalidOutputType

Now, compile the file with the following command in your matlab cli:

mcc -m test_cpu.m -o test_cpu

To run the executable, you will get these errors:

./test_cpu: error while loading shared libraries: libmwlaunchermain.so: cannot open shared object file: No such file or directory

./test_cpu: error while loading shared libraries: libmwmclmcrrt.so.9.0.1: cannot open shared object file: No such file or directory

You can find these libraries with locate command and and them to your LD_LIBRARY_PATH:

locate libmwlaunchermain
/usr/local/MATLAB/R2016a/bin/glnxa64/libmwlaunchermain.so
locate libmwmclmcrrt.so.9.0.1
/usr/local/MATLAB/R2016a/runtime/glnxa64/libmwmclmcrrt.so.9.0.1

export LD_LIBRARY_PATH=/usr/local/MATLAB/R2016a/bin/glnxa64/:/usr/local/MATLAB/R2016a/runtime/glnxa64/

Now, you could run your executable with an argument:
./test_cpu 3000

Enjoy it!


Comments

Popular posts from this blog

How to fix Android when developer options are not available for this user

How to install Matlab without GUI

Contiki 3.0 on XM1000