C++调用matlab引擎实现三维图的绘制
VS2012设置
项目–项目属性–配置属性–VC++目录–包含目录 D:\MATLAB\R2016a\extern\include
项目–项目属性–配置属性–VC++目录–库目录
D:\MATLAB\R2016a\extern\lib\win64\microsoft
添加依赖项有两种方法:
方法一:项目中设置
项目–项目属性–配置属性–链接器–输入–添加依赖项
libmx.lib
libmat.lib
libmex.lib
mclmcr.lib
mclmcrrt.lib
libemlrt.lib
libeng.lib
libfixedpoint.lib
libcovrt.lib
方法二:程序中添加
#pragma comment(lib,“libmx.lib”)
#pragma comment(lib,“libmat.lib”)
#pragma comment(lib,“libmex.lib”)
#pragma comment(lib,“mclmcr.lib”)
#pragma comment(lib,“mclmcrrt.lib”)
#pragma comment(lib,“libemlrt.lib”)
#pragma comment(lib,“libeng.lib”)
#pragma comment(lib,“libfixedpoint.lib”)
#pragma comment(lib,“libcovrt.lib”)
计算机–属性–高级系统设置–环境变量–系统变量–Path–新建
D:\MATLAB\R2016a\bin\win64
matlab程序
pt.m文件
clc;clear;close all; % 定义点(x,y,z) x = randn(50,1); xmax = max(x); xmin = min(x); y = randn(50,1); ymax = max(y); ymin = min(y); z = exp(sin(x.^2)) + exp(cos(y.^2)); N = 500; % 每个维度的数据点数 % 网格化x,y二维空间 [X,Y] = meshgrid(linspace(xmin,xmax,N),linspace(ymin,ymax,N)); % 采用插值法扩展数据,可用方法有'linear'(default)|'nearest'|'natural'|'cubic'|'v4'| Z = griddata(x,y,z,X,Y,'v4'); %% 等高线法 figure('NumberTitle','off','Name','等高线法','Color','w','MenuBar','none','ToolBar','none'); contourf(X,Y,Z,N, 'LineColor','none'); colormap('jet'); colorbar; axis off; %{ %% 投影图法 figure('NumberTitle','off','Name','投影图法','Color','w','MenuBar','none','ToolBar','none'); surf(X,Y,Z,'LineStyle','none'); xlim([min(X(:)) max(X(:))]); ylim([min(Y(:)) max(Y(:))]); axis off; colormap('jet'); colorbar; shading interp; view(0,90); %% imagesc法 figure('NumberTitle','off','Name','imagesc法','Color','w','MenuBar','none','ToolBar','none'); % 因为图像坐标和笛卡尔坐标起始位置不一样,需要上下翻转 imagesc(flipud(Z)); colormap('jet'); colorbar; axis off; %% pcolor法 figure('NumberTitle','off','Name','pcolor法','Color','w','MenuBar','none','ToolBar','none'); pcolor(X,Y,Z); colormap('jet'); colorbar; shading interp; axis off; %}
VS2012控制台程序
matlab.cpp文件
#include "stdafx.h" #include <stdlib.h> #include <stdio.h> #include <string.h> #include "engine.h" // 方法一 #pragma comment(lib,"libmx.lib") #pragma comment(lib,"libmat.lib") #pragma comment(lib,"libmex.lib") #pragma comment(lib,"mclmcr.lib") #pragma comment(lib,"mclmcrrt.lib") #pragma comment(lib,"libemlrt.lib") #pragma comment(lib,"libeng.lib") #pragma comment(lib,"libfixedpoint.lib") #pragma comment(lib,"libcovrt.lib") int _tmain(int argc, _TCHAR* argv[]) { Engine *ep; if (!(ep = engOpen("\0"))) { fprintf(stderr, "\nCan't start MATLAB engine\n"); return EXIT_FAILURE; } //隐藏matlab命令窗口 engSetVisible(ep, 0); /* // 测试 engEvalString(ep, " clc;clear;close all;\ % 定义点(x,y,z)\ x = randn(50,1);\ xmax = max(x);\ xmin = min(x);\ y = randn(50,1);\ ymax = max(y);\ ymin = min(y);\ z = exp(sin(x.^2)) + exp(cos(y.^2));\ N = 500; % 每个维度的数据点数\ % 网格化x,y二维空间\ [X,Y] = meshgrid(linspace(xmin,xmax,N),linspace(ymin,ymax,N));\ % 采用插值法扩展数据,可用方法有'linear'(default)|'nearest'|'natural'|'cubic'|'v4'|\ Z = griddata(x,y,z,X,Y,'v4');\ figure('NumberTitle','off','Name','等高线法','Color','w','MenuBar','none','ToolBar','none');\ contourf(X,Y,Z,N, 'LineColor','none');\ colormap('jet');\ colorbar;\ axis off;\ "); */ /* // 测试 engEvalString(ep, "figure;"); */ /* // 切换至 pt.m 所在文件夹 engEvalString(ep, "cd C:\\Users\\Administrator\\Desktop\\matlab\\figure; "); // 运行 pt.m engEvalString(ep, "run pt"); */ engEvalString(ep, "cd C:\\Users\\Administrator\\Desktop\\matlab\\figure;\ run pt;\ "); printf("按任意键继续\n"); fgetc(stdin); engEvalString(ep, "close;"); engClose(ep); return EXIT_SUCCESS; }
运行结果
说明
VS2012新建名称为“matlab”项目,在“matlab”项目文件夹下新建figure文件夹,figure文件夹中放matlab画图程序 pt.m。
VS2012编译平台选择与matlab版本对应起来,文章选择的是64位。
到此这篇关于C++调用matlab引擎实现三维图的绘制的文章就介绍到这了,更多相关C++ matlab绘制三维图内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
最新评论