MATLAB calls C functions

It is possible to call the C function library in MATLAB, but you must first configure the environment and compiler, and write an interface function to connect C and MATLAB.

The function of this mex function is to easily call the ready-made C function library, or rewrite the inefficient and energy-consuming department MATLAB code into C substitute.

 

  1. Install C compiler

MATLAB itself cannot compile the C language and needs the support of other compilers.

MinGW-w64 used here, note: the MATLAB version needs to correspond to the MinGW-w64 version. Introduction in the MATLAB community:

https://ww2.mathworks.cn/matlabcentral/fileexchange/52848-matlab-support-for-mingw-w64-c-c-compiler

 

The online installation tool is very rubbish and I can't download it, and I can't hang up a ladder. It can also be installed with an offline package, just unzip it to the desired path, the 6.3 version of the offline package link:

https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/6.3.0/threads-posix/seh/x86_64-6.3.0-release-posix-seh-rt_v5-rev2.7z/download

Environment variable configuration: (The path should be changed according to your own file path)

 

 

 

Install MinGW in MATLAB:

Run setenv('MW_MINGW64_LOC',folder) on the Matlab command line. Folder is the installation location of MinGW (the same as the environment variable path), and single quotes are required;

 

Check whether MinGW is installed successfully:

Run mex -setup on the Matlab command line, the following is successful

 

 

  1. Interface file writing, compiling, calling

Interface file elements:

The interface file is a file used to associate the C environment and the MATLAB environment. The file type is .c. The most checked template of the file is as follows:

#include "mex.h"
void  mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])
{
    return;
}

 

The function name mexFunction  and its parameters are fixed and cannot be changed. The interface conversion is done in this function, and the C function we need is called to complete the function.

 

File compilation:

If there are multiple C files in the path that need to be compiled, use this compilation command:

mex -output  mex_compress  *.c  % compile all C files to generate mex_compress binary files

 

Interface function call:

Only the mexFunction function in the file can be called in MATLAB, and then other function functions can be called in this function. But you need to pay special attention: when calling this function in MATLAB, the function name is not mexFunction, but the file name! For example, when the file is named hello.c, the calling command is [c,d] = hello(a,b);

 

For more information, please refer to the official website API description

API:https://ww2.mathworks.cn/help/matlab/cc-mx-matrix-library.html?s_tid=CRUX_lftnav

 

 

  1. Transfer of interface parameters

Parameter concept:

The parameters of the interface can be passed in both directions (pointer mode) just like C. There are 4 formal parameters in the interface:

int  nlhs, mxArray * plhs [], int  nrhs, const mxArray * prhs []

It’s a little difficult to understand this parameter, so the concept is clear step by step

First call the incoming level: when MATLAB calls the interface, the input parameters are not mapped to these 4 formal parameters one by one

Parameter definition: nlhs output parameter number, plhs [] output parameter value, nrhs input parameter number, prhs [] input parameter

(L means left output, R means right input)

Parameter form: The parameter values plhs [] and prhs [] are actually an address, and the address supports multiple parameters, and each parameter can be in any format, including a multi-dimensional matrix, so the actual parameters of the interface are The number can be expanded almost infinitely.

 

Parameter input:

The parameters are input through two interfaces, nrhs inputs a value = total number of parameters, plhs[] inputs a pointer, the first parameter is plhs[0], and the second parameter is plhs[1]. Each parameter can be of any type, including matrices. When nrhs=2, it means that plhs[0] and plhs[1] have data, and plhs[2] has no data. The method of obtaining parameter data is as follows:

unsigned char *idata;

idata = mxGetPr(prhs[0]); //Get a pointer to the matrix

mexPrintf("idata[0] = %d\n",idata[0]);

 

Parameter output:

The way of sending data out in the function is similar to the input, plhs[] is the address of the outgoing parameter, but nlhs does not need us to operate. The data transfer method is as follows:

First create a data type on the plhs[0] address (create a uint32 type here, please refer to the official website for more details)

plhs[0] = mxCreateUninitNumericMatrix(1, 1, mxUINT32_CLASS, mxREAL);

Then get the pointer of plhs[0] through mxGetPr;

unsigned long *out_ptr = mxGetPr(plhs[0]);

Then connect the pointer out_ptr of the parameter plhs[0] to the C function

My_C_function(out_ptr);

 

Obtain the data from the mexFunction function in the MATLAB file :

[c, d] = mex_helloworld(a, b);

c and d are the two outgoing parameters, corresponding to plhs[0] and plhs[1]

 

About mexFunction function:

This function is only used for the interface between MATLAB and C. The functions such as interface conversion and inspection are implemented in the function. It is not necessary to write the C function realization program in this function. The correct way is to call the pure C function in this function. For example, this function does not support the printf function. Instead , the mexPrintf function in the mex function of MATLAB is used . The C function called in the function supports the complete C function.

 

Guess you like

Origin blog.csdn.net/a1254484594/article/details/109282955