VC + + How to draw with matlab2014b's graphics engine

Source: Internet
Author: User

VC + + How to take advantage of MATLAB graphics engine

The detailed process of drawing using the graphics engine provided by matlab2014b in a Visual C + + 2015 project.

Source of the problem:

Sometimes in C + + to write some demo program, there is the need for data visualization. In general, there are just a few solutions under window: Using a framework like MFC,QT, using a system API like GDI, or using the DirectX API or open GL. The non-mainstream of the data written in JSON as a private format, and then the front-end technology blahblah such as the way the web is visualized. Of course, you can also use some other software to provide the API, such as Matlab.

Why is there such a demand? First of all the above-mentioned things, used to do visual demonstration can not be called repeating the wheel, it is planted rubber tree. If you've seen the directx2d on MSDN with a rectangular presentation, you'll know what it's like to have an egg ache, filled with details, and if it's just for an algorithm demo, the simulation stuff is really overkill and the egg hurts. MATLAB's advanced graphics engine has long provided a complete set of solutions, not really a pity. Especially matlab2014b inside, the graphics engine has changed, drawing out the picture is much more beautiful than before. It's not much easier to do a demo.

The effect of the new graphics engine:

Pre-Preparation:

My test environment is: Window Technical Preview (WIN10) + Visual Studio Preview + Matab 2014 64-bit; Of course, the win8+vs2013 combination is certainly possible.

I guess most people who have tried this kind of drawing have fallen into the platform, compiling, loading, and linking the pits. So share the pit experience so that others can make less detours.

Principle:

The MATLAB graphics engine is invoked in C + +, mainly using COM Component Services. Use Matlab as a COM Server to accept requests from the client application. The M script of Matlab is an explanatory language, so most calls are implemented directly by passing the Eval method of the command string. Then the specific process, that is, the user's C + + program, include the MATLAB provided by the header file engine.h, MATLAB provides a reference to the static library file libeng.lib,libmx.lib, MATLAB provides a reference to the dynamic link library files (located in .... A list of DLL files in the. \matlab\r2014b\bin\win64). header file, static library file, dynamic link library file is what, this is not popular. Recommend "self-cultivation of programmers".

Specific process:

Needless to say, let's try it out, first put the test code out.

1#include <cstdlib>2#include <cstdio>3#include <cstring>4#include"engine.h"5 6 Const intBuffer_size =1024x768;7 CharBuffer[buffer_size];8 9 voidTest ()Ten { Oneengine*EP; AMxarray *x1 =NULL; -Mxarray *y1 =NULL; -     if(ep = Engopen ("")) ==NULL) the     { -printf"Engine Fail"); -     } - Engoutputbuffer (EP, buffer, buffer_size); +printf"Init Success"); -  +     Doublex[5] = {1.0,2.5,3.7,4.4,5.1 }; A     Doubley[5] = {3.3,4.7,9.6,15.6,21.3 }; atX1 = Mxcreatedoublematrix (1,5, mxreal); -Y1 = Mxcreatedoublematrix (1,5, mxreal); -  -memcpy ((void*) MXGETPR (x1), (void*) x,sizeof(x)); -memcpy ((void*) MXGETPR (y1), (void*) Y,sizeof(y)); -  inEngputvariable (EP,"x", X1); -Engputvariable (EP,"y", y1); to  +Engevalstring (EP,"plot (x, y)"); - GetChar (); the Engclose (EP); * } $ Panax Notoginseng intMain () - { the test (); +}

But don't worry, just copy and paste into a new project the direct build is definitely fail. In fact, the configuration of project properties is much more troublesome than simply writing code.

The first step:

Confirm that your version of MATLAB is 64-bit or 32-bit. 32 bit do not change, but the general Matlab is loaded with 64 bits of it? Then, create a new profile in Build–configuration Manager: Active solution Paltform Active platform Remember to select x64, select Copy from Win32 without it.

This step is a big pit, because most Visual C + + programs are compiled with 32-bit platforms as targets, so if there are 32-bit platform-related code, build will pee. So pay attention to the library you use, and if you know what you're doing, it's best.

Step Two:

Set up VC + + directory. In the Solution Manager, select the current project, right-click Menu-Properties, or direct alt+enter to open the Properties panel. In short, select the VC + + directory, on the right will come out a lot of things you want to fill. There are three things to focus on: The first executable directory, the second contains the directory, the fourth, the Library directory. These three things correspond to the location of the DLL file, the location of the header file, and the location of the Lib file.

Specific location, you can see in the picture, my MATLAB is installed in the default location, install to another location can change the prefix. These directory addresses tell the compiler and linker where to find what to use.

C:\Program Files\matlab\r2014b\bin\win64;

C:\Program Files\matlab\r2014b\extern\include;

C:\Program Files\matlab\r2014b\extern\lib\win64\microsoft;

Look at the name and you'll know, Bin,include,lib.

Step Three:

C + + has a bad problem, you have to manually tell the linker what library files I used. Above the repository directory there just tell the linker, if you need a library file, you come to these places to find. But the linker does need to know the name of the library you're using. There are two libraries needed for MATLAB drawing: Libeng.lib and Libmx.lib. One is the MATLAB engine, one is the matrix library of Matlab. How do you do that? In the project properties, select Linker (linker), Input add two to the additional dependencies on the right: Libeng.lib, Libmx.lib.

Fourth Step

Basically changed the platform, filled the table of contents, filled in the name of the library, it can be all right. But sometimes there will always be a variety of things, such as the hint Libeng.dll can not find AH and so on. At this point, you can fix the problem by modifying the environment variables. C:\Program Files\matlab\r2014b\bin\win64; Add the original executable directory to the system's PATH environment variable, then remember to restart ... . can solve the problem.

Fifth Step

Configuration problem has been solved, now finally can try the skill. Create a new CPP file in the project, and copy the code in. Run for a second look? MATLAB's C engine API is very simple. The few ways to count are the ones that start with Eng:

Engopen, Engclose know what to do with a look. Engputvariable, enggetvariable is a variable that is used to access the workspace. Then there is the almighty engevalstring. The parameter is an engine pointer and a command string. Just like in the MATLAB inside the command that knocks.

Then if need to use get/put Variable, need to use the matrix library Libmx.lib content, is to create a double array, create a Mxarray object, a look can understand what is it, the details of the contents of the doc in Matlab, do not repeat.

Oh, isn't that great? Although it's just a simple effect. But with good but is magical infinite ah, you can write a class packaging for the convenience of later use.

In addition, there is a little trick. Every time you run your applet, it will always open a new instance of MATLAB COM server. The cold starts 10 seconds faster, and the hot start takes several seconds. It's really slow, yes. One solution is to start a matlab Automation Server. So each time you run the program, it will actively link to this manually launched instance on the request service instead of making a new one out, it will be much faster.

How do you do it? Very simple, start matlab, add a command line parameter/automation. You can enter matlab/automation in cmd or PowerShell.

That's probably it.

VC + + How to draw with matlab2014b's graphics engine

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.