MATLAB generates dll library and calls and parameter settings in Qt

Navigation

  • Matlab generates dll and calls it in Qt
    • Matlab generates dll
    • Qt calls the generated dll
    • dll library input and output parameters
    • My experience when using it
    • Reference link

Matlab generates dll and calls it in Qt

Manually converting Matlab algorithms into C++ is time-consuming and laborious, and the C/C++ code generated by matlab coder is very complex. In order to improve development efficiency, you can use Matlab to generate a dynamic library dll and then call it in Qt.

Matlab generates dll

  1. First, make sure that the .m file to be converted is a complete function with input and output. All dependent third-party library functions are best placed in this file. Using a main.m file to call this function can run smoothly. The general structure is as follows
function [outA, outB] = calcOne(a, b,c)
%Main function body .m file function with the same name
end

function [x] = ls(aa, bb)
% Other functions used
end
  1. Configure the mingw compiler, go to the official matlab website to find the corresponding mingw version, link: mingw version corresponding to the matlab version
  2. MinGW download link or the MinGW link corresponding to the earlier version of matlab
  3. Install mingw into a directory you know (it is best not to have spaces or Chinese characters in the directory), then configure the system environment variables, and add the location of the “bin” folder in MinGW-w64 to the path of the system environment variables. Refer to step
  4. Add the path of mingw to the environment variable of matlab and enter it on the matlab command line
setenv(MW_MINGW64_LOC’,C:\TDM-GCC-64’) % Change the path according to your own
mex-setup C++

The following display indicates success
result

  1. Enter the deploytool command on the matlab command line and select the Library Compiler option
    MATLAB Complier

  2. Select C++ Shared Library, click the plus button on the right, select the source file that needs to generate the dll library, and finally click the Package option to generate the dll file
    Generate dll

  3. In the generated file directory, go to the for_redistribution_files_only directory, you can see the generated dll file, copy the *.dll, .h, .lib file to the qt project directory .

  4. At this point, the dll library is completed

Dll generated by Qt call

  1. Configure the relevant library functions of matlab at the end of the Qt pro file
INCLUDEPATH + = 'F:/Program Files/MATLAB/R2021b/extern/include'
INCLUDEPATH + = 'F:/Program Files/MATLAB/R2021b/extern/include/win64'

win32: LIBS + = -L'F:/Program Files/MATLAB/R2021b/extern/lib/win64/mingw64/' -llibmex
win32: LIBS + = -L'F:/Program Files/MATLAB/R2021b/extern/lib/win64/mingw64/' -llibmx
win32: LIBS + = -L'F:/Program Files/MATLAB/R2021b/extern/lib/win64/mingw64/' -llibmat
win32: LIBS + = -L'F:/Program Files/MATLAB/R2021b/extern/lib/win64/mingw64/' -llibeng
win32: LIBS + = -L'F:/Program Files/MATLAB/R2021b/extern/lib/win64/mingw64/' -lmclmcr
win32: LIBS + = -L'F:/Program Files/MATLAB/R2021b/extern/lib/win64/mingw64/' -lmclmcrrt

INCLUDEPATH + = 'F:/Program Files/MATLAB/R2021b/extern/lib/win64/mingw64'
DEPENDPATH + = 'F:/Program Files/MATLAB/R2021b/extern/lib/win64/mingw64'

INCLUDEPATH + = 'F:/Program Files/MATLAB/R2021b/extern/lib/win64'
DEPENDPATH + = 'F:/Program Files/MATLAB/R2021b/extern/lib/win64'

INCLUDEPATH + = 'F:/Program Files/MATLAB/R2021b/extern/lib/win64/microsoft'
DEPENDPATH + = 'F:/Program Files/MATLAB/R2021b/extern/lib/win64/microsoft'

***F:/Program Files/MATLAB/R2021b/***
Replace with your own Matlab path
  1. Add the .h header file and add the .h file of the dynamic library to the project
  2. Add a dynamic library, add the dynamic library file to the project, project folder-add library-external library-click library file selection, cancel the useless platform check, cancel windows-add ‘d’ as a suffix for the debug version, click Next Add to
  3. Initialize the called dll library, add the .h header file to the file that needs to call the external library, and add such a statement to the constructor of this class.
// This calcOne is the name of the library function, change it as needed
if(calcOneInitialize()) {
qDebug() << "calcOne init successfully!";
} else {
qDebug() << "calcOne init failed!";
}
  1. Set the input and output parameters of the function. For details, see Section 3 Input and Output Parameter Settings
  2. To pass parameters into the function, you can view the function definition according to the .h file of the dynamic library, and then pass the parameters and call them where needed.
int nargout = 2;//The number of output parameters
calcOne(nargout, outA, outB, a, b);//The number of outgoing parameters, output parameters, input parameters

dll library input and output parameters

In fact, many people have shared the previous content, but I have not seen much about how to pass parameters and call functions, so I have taken many detours. Now I will record the settings of some parameters I used.

The first thing to explain is that the dll library function generated by matlab needs to specify the specific variable type of matlab when passing parameters. In C++, mwArray is generally used. This is the bridge between C++ and matlab. It is in the form of an array, even if it is just one The double variable must also be specified as a 1*1 mwArray.

The following explains how different data is initialized and assigned values by category.

  1. Single int, double, etc.
mwArray a(1,1, mxDOUBLE_CLASS,mxREAL);//The matrix is declared as 1*1, the variable type is double, and the data type is real real number
double* aValue = new double[1];//The variable used for assignment must be specified as such a one-dimensional array
aValue[0] = 3.0; // Assign a value to this variable
a.SetData(aValue, 1);//Assign a value to mwArray. The second parameter is the length of the data to be passed.

  1. Two-dimensional array
mwArray b(2,3, mxDOUBLE_CLASS,mxREAL);//Declared as a 2*3 matrix, the variable type is double, and the data type is real.
double* bValue = new double[6];// The variable used for assignment must be specified as this one-dimensional array, and the length is the number of elements of the matrix, that is, 2*3=6
for (int i = 0; i < 6; i + + ) bValue[i] = i;
b.SetData(bValue, 6);//Assign a value to mwArray. The second parameter is the length of the data to be passed.

There is a point to note here. When setting Data, values are assigned column by column in vertical order.
So there are two ways of thinking:

  • When bValue is set, the order must be arranged in advance so that the result after b is set is the ideal result.
  • First transpose b to 3*2, SetData, and then in the matlab function, transpose before using this variable.

No matter which method is used, it is best to add disp(b) to the matlab function. The result will be displayed in the Qt output and check that the parameters passed are correct. I think passing parameters is the most difficult and error-prone part. No matter what parameters are passed, check them again.
I haven’t used three-dimensional arrays before, so explore on your own.
3. Pass the cell matrix

mwArray cellArray(1, 3, mxCELL_CLASS);//Declaration
//Each element is a cell
for (int col = 1; col <= 3; col + + ) {
// Each cell is a 2*1 matrix
mwArray ccc(2, 1, mxDOUBLE_CLASS, mxREAL);
double* data = new double[2 * 1];
for (int xxx = 0; xxx < 2; xxx + + ) {
data[xxx] = 2;
}
ccc.SetData(data, 2);
cellArray.SetData(col) = ccc; // Assign value at specified position
}
  1. Outgoing parameters
mwArray cellArray(mxDOUBLE_CLASS, mxREAL);//Outgoing parameters can only declare variable types without specifying dimensions

Experiences during use

  1. It still took a few days to complete this whole thing. Configuring MinGW is time-consuming, and debugging errors when calling library functions is also time-consuming. Therefore, I have the following suggestions.
  2. First use the simplest matlab function for testing, and test this process in a demo project to see if it can output the expected results.
  3. First set the simplest set of input parameters and call the function. Starting from the converted matlab function, first disp(a) to see if the parameters passed are correct and the calculation results are correct.
  4. Call this library in the main project file

Reference link

  • MATLAB MinGW-w64 installation and use

  • Qt calls the dll library generated by matlab

  • QTCreater calls the .dll file generated by MatLab