0
点赞
收藏
分享

微信扫一扫

C++调用matlab引擎画三维图

年夜雪 2022-12-23 阅读 191

VS2012设置

添加依赖项有两种方法:

方法一:项目中设置
方法二:程序中添加

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;
}

运行结果

在这里插入图片描述

说明

在这里插入图片描述

举报

相关推荐

0 条评论