
It's time for diplomas and I want to share my recipe for its implementation. So: the topic is complex, time is short, to write everything from scratch or to search for ready-made libraries - there is no great desire, especially since the speed of the system is important for me, and you can expect any trick from the left self-defined libraries.
I must say that I did not think long and came to the following conclusions:
- The operating system didn’t have to think too much: I am a fan of Debian (by the way: my solution can be transferred to Windows).
- The interface of the future program - C ++, Qt.
- The logic of the program - MatLab.
Training
For the operation of the system we need the following components:
- The so-called MATLAB Compiler Runtime . This is a free distribution of MatLab dynamic libraries. You can download it from the official site and use it at your discretion.
- Actually Qt itself. It is downloaded from Linux repositories, or from the official site.
')
Installation
MATLAB Compiler Runtime is distributed in a zip archive. Inside you will find the install.sh file. After its launch, the graphical installer will open, which in 3 clicks will do its job.
Qt comes from repositories. If you downloaded the fresh version from the site, then you must perform the following steps:
Under the root:
mkdir /opt/QtSDK chown %username% /opt/QtSDK
Further installation must be made on behalf of the user and in the / opt / QtSDK directory
With the installation finished, go to the settings:
It is necessary to register libraries in the system, for this:
echo /usr/local/MATLAB/MATLAB_Compiler_Runtime/v716/runtime/glnxa64 >> /etc/ld.so.conf.d/matlabLibs.conf echo /usr/local/MATLAB/MATLAB_Compiler_Runtime/v716/sys/os/glnxa64 >> /etc/ld.so.conf.d/matlabLibs.conf echo /usr/local/MATLAB/MATLAB_Compiler_Runtime/v716/sys/java/jre/glnxa64/jre/lib/amd64/native_threads >> /etc/ld.so.conf.d/matlabLibs.conf echo /usr/local/MATLAB/MATLAB_Compiler_Runtime/v716/sys/java/jre/glnxa64/jre/lib/amd64/server >> /etc/ld.so.conf.d/matlabLibs.conf echo /usr/local/MATLAB/MATLAB_Compiler_Runtime/v716/sys/java/jre/glnxa64/jre/lib/amd64 >> /etc/ld.so.conf.d/matlabLibs.conf ldconfig
And set the environment variable XAPPLRESDIR (requires root rights):
export XAPPLRESDIR="/usr/local/MATLAB/MATLAB_Compiler_Runtime/v716/X11/app-defaults"
Next, you need to copy the file: / usr / local / MATLAB / MATLAB_Compiler_Runtime / v716 / X11 / app-defaults / Matlab to the user's home directory. In general, in theory, this is not necessary, but without this, nothing works. This is a known bug, but it has not yet been fixed.
I will walk a bit along the paths to the libraries:
- The / usr / local / MATLAB / MATLAB_Compiler_Runtime / part may vary depending on where you installed the MCR.
- Part v716 / is a version of MatLab itself and it can also change.
- Part glnxa64 / for 64 bit OS and glnxa32 for 32 bit OS.
And in general. All these paths will be written on the final stage of the MCR installation, but I do not advise copying them, and I propose to double-check and rewrite everything manually (I don’t know what this is all about, but on two different machines I have stupid copies of these paths that didn’t work). Also after installation there will be one more way: / usr / local / MATLAB / MATLAB_Compiler_Runtime / v716 / sys / os / glnxa64, I do not advise you to prescribe it: after that, they refused to start all the programs, and without it everything works fine.
Uncover MatLab
Now you can write the logic of the future application. Create a new m-file and write a new function. We will have a simple function: we will summarize two transferred arrays of numbers:
function [out] = SUM(in1, in2) out = in1 + in2; end
Everything is ready to compile this function into a dynamic library:
- We write in the MatLab console: deploytool.
- In the window that opens, write the name of the project, its location and from the drop-down list, select the C Shared library and click OK. Note that in Linux, library names begin with the word lib, therefore the project should be called libsum.prj
- In the Exported Functions window, click on Add files and add the function you just created.
- Click on the Build button and wait for the end of the compilation.
After compilation, the distrib folder will be in the project folder, in which our library and the header file will be located.
This completes the work with MatLab.
Uncover Qt
We start Qt and we create the new project.
In the pro file, we write the paths to the libraries and header files:
INCLUDEPATH += /usr/local/MATLAB/MATLAB_Compiler_Runtime/v716/extern/include LIBS += -L/usr/local/MATLAB/MATLAB_Compiler_Runtime/v716/bin/glnxa64/ -leng -lm
In the project itself, we write the following code:
#include <QtCore/QCoreApplication> #include <libsum.h> #include <iostream> using namespace std; int main(int argc, char *argv[]) { cout<<"Loading..."<<endl; if (!libsumInitializeWithHandlers(NULL, 0)){ cout<<"Could not initialize the application."<<endl; return -1; } if (!libsumInitialize()){ cout<<"Could not initialize the library."<<endl; return -2; } mxArray *in1 = mxCreateDoubleMatrix(2, 1, mxREAL); mxArray *in2 = mxCreateDoubleMatrix(2, 1, mxREAL); mxArray *out = mxCreateDoubleMatrix(2, 1, mxREAL); double *i1 = mxGetPr(in1); double *i2 = mxGetPr(in2); i1[0] = 1; i1[1] = 2; i2[0] = 3; i2[1] = 4; if(!mlfSUM(1, &out, in1, in2)){ cout<<"Could not execute function."<<endl; } double *res = mxGetPr(out); cout<<res[0]<<"; "<<res[1]<<endl; mxDestroyArray(in1); mxDestroyArray(in2); mxDestroyArray(out); cout<<"Terminating..."<<endl; libsumTerminate(); return 0; }
That's all. You can run.
findings
The diploma is completed, the program is working, I am a happy information technology engineer. It seems to be all well and good, but there are a couple of nuances:
- The size of the MCR archive is 874 MB.
- After launching the program, the MatLab libraries start loading and it postpones the launch for a couple of seconds.
True, there are pluses:
- You can focus your attention on the main work without worrying about the optimization of side functions and catching errors in them.
- The speed of MatLab functions is much higher than what you could achieve in 3 months of working on a project.
- You get all the power of MatLab in your application.
- Time to work on the project is reduced.
If I’m interested, I’ll write how I recognized voice commands in this way using neural networks.