Engine Operation Details of VC ++ and Matlab hybrid programming (2) engine API details

Source: Internet
Author: User

3. Engine API details

Before calling the Matlab Engine, add a line in the relevant file: # include "enging. H", which contains the description of the engine API function and the definition of the required data structure. The engine functions that can be called in VC are as follows:

3.1 enable and disable the engine

Engopen-open Matlab Engine


Function declaration:

Engine * engopen (const char * startcmd );

The startcmd parameter is a string parameter used to start the Matlab Engine. It can only be null in windows.

The function return value is an engine pointer, which is the engine data structure defined in engine. h.

Engclose-Disable the Matlab Engine

Function declaration:

Int engclose (engine * EP );

The EP parameter indicates the engine pointer to be disabled.

If the return value is 0, the function is disabled successfully. If the return value is 1, an error occurs.

For example, the code used to open/close the Matlab Engine is as follows:

Engine * EP; // defines the Matlab Engine pointer.

If (! (Ep = engopen (null) // test whether the Matlab Engine is successfully started.

{

MessageBox ("can't start Matlab Engine! ");

Exit (1 );

}

.............

Engclose (EP); // disable the Matlab Engine.

3.2 send command strings to MATLAB

Engevalstring-send command for MATLAB to execute.

Function declaration:

Int engevalstring (engine * EP, const char * string );

The EP parameter is the engine pointer returned by the engopen function, and the string is the command to be executed by Matlab.

If the return value is 0, the execution is successful. If the return value is 1, the execution fails (for example, the command cannot be correctly interpreted by MATLAB or the Matlab Engine has been disabled ).

3.3 obtain the output of the MATLAB command window

To obtain the command string sent by the engevalstring function in VC and output it in the MATLAB window after being executed by MATLAB, you can call the engoutputbuffer function.

Function declaration:

Int engoutputbuffer (engine * EP, char * P, int N );

The EP parameter is the Matlab Engine pointer, p is the buffer used to save the output structure, and N is the maximum number of characters stored, usually the size of the buffer p. After the function is executed, the command line output result caused by the subsequent engevalstring function is saved in buffer p. To stop saving, you only need to call the code: engoutputbuffer (Ep, null, 0 ).

3.4 read and write MATLAB data

3.4.1 obtain variables from the Matlab Engine workspace.

Mxarray * enggetvariable (engine * EP, const char * Name );

The EP parameter is the opened Matlab Engine pointer, And the name is the array name specified in the string form.

The function return value is a pointer to the name array and the type is mxarray * (mxarray data type is described in Section 4th ).

3.4.2 write variables to the Matlab Engine workspace.

Int engputvariable (engine * EP, const char * Name, const mxarray * MP );

The EP parameter is the open Matlab Engine pointer, the MP is the pointer to the written variable, and the name is the variable name written into the Matlab Engine workspace.

If the return value is 0, the variable is successfully written. If the return value is 1, an error occurs.

3.5 display/hide the main MATLAB window when calling the engine

By default, the main MATLAB window is opened when Matlab is called in the engine mode, where you can operate it at will. However, it may also interfere with the running of the application. You can use the following settings to check whether the window is displayed.

Int engsetvisible (engine * EP, bool value );

The EP parameter is the opened Matlab Engine pointer, and the value is the identifier of whether to display. The value true (OR 1) indicates that the MATLAB window is displayed, and the value false (or 0) indicates that the MATLAB window is hidden.

If the return value is 0, the setting is successful. If the return value is 1, an error occurs.

To obtain the display/hide information of the current MATLAB window, you can call the function:

Int enggetvisible (engine * EP, bool * value );

The EP parameter is the opened Matlab Engine pointer, and the value is the variable used to save the display/hide situation (transmitted using the pointer method ).

If the return value of the function is 0, the result is obtained successfully. If the return value is 1, an error occurs.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.