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 #include #include #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位。