Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of MEX File in MATLAB

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you the example analysis of MEX files in MATLAB, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Text:

Introduction to 1.mex Fil

Mex is an acronym for matlab executable, which means it can be executed in matlab.

Through C or Fortan language development, after compilation, the generated object files can be called and executed by the M language interpreter.

The use of the mex file is similar to the m file, but the m file execution takes precedence.

The structure of 2.mex source files

/ / the structure of the mex source file. The standard writing is # include "mex.h" / / void mexFunction (int nlhs, mxArray * plhs [], int nrhs, const mxArray * prhs []) {} must be added.

Let's say that the main function in the programming of C + + language provides the interface between the operating system and C language subroutines.

Then the function of the mexFunction function is to provide an interface between the MATLAB subroutine and the Cmax Cobb + subroutine.

# include "mex.h" / / must be added.

MexFunction is not the routine you called.

Instead, mexFunction is the name of the gateway function required by every MEX function in C. When the MEX function is called, MATLAB ®finds and loads the corresponding MEX function with the same name.

MATLAB then searches for a symbol named mexFunction in the MEX function. If one is found, it calls the MEX function with the address of the mexFunction symbol.

If MATLAB cannot find a routine named mexFunction in the MEX function, an error message is displayed.

Four parameters are used to output and input data, respectively:

Nlhs is the number of output parameters

Plhs is the output parameter pointer

Pointer array to the desired mxArray output parameters

Nrhs is the number of input parameters

Prhs is the input parameter pointer

An array of pointers to the mxArray input parameters.

Do not modify any PRHS values in the MEX file.

Changing the data in these read-only mxArrays can have adverse side effects.

Note:

Both the output and input parameters are operated by pointers.

An array of pointers to the mxArray input parameters. Do not modify any PRHS values in the MEX file. Changing the data in these read-only mxArrays can have adverse side effects.

3. Debug mex files

For more complex functions, and is achieved through mex files, it is troublesome to debug mex files in the MATLAB environment. At this time, we should use vs (C development environment) to debug. Note:

I use VS2017 myself, and the vs I refer to below is vs2017 by default, but there are only slight differences in the use of other versions.

For example, the steps are as follows:

1) write our test file

/ / demo_mextest.c#include "mex.h" / / must add void mexFunction (int nlhs, mxArray * plhs [], int nrhs, const mxArray * prhs []) / / mexFunction standard {printf ("hello mex world!\ n"); mexPrintf ("hello mex world!\ n");}

2) in order to debug mex in vs and compile, note that "- g" is added here.

% matlab window run results > > mex demo_mextest.c-g is compiled with 'Microsoft Visual C++ 2017 (C)'. MEX completed successfully.

3) attach processes

4) in VS, set a breakpoint in the program to observe the results of the process.

In VS, the method to set the breakpoint, right-click at the code where you want to set the breakpoint, and select the breakpoint.

5) in the command line window of matlab, run the mex file.

The result of the run will be paused at the breakpoint as we have set, so you can use this method to debug the mex file program.

% matlab window run result > > demo_mextesthello mex worldview hello mex world!

In addition, it is worth noting that during debugging, VS and MATLAB are associated, and you can not operate matlab until the code continues to run, otherwise, the MATLAB interface will not respond at the breakpoint.

The above is all the contents of the article "sample Analysis of MEX Files in MATLAB". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report