日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C++調用matlab引擎實現三維圖的繪制_C 語言

作者:楊錚... ? 更新時間: 2023-02-02 編程語言

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

原文鏈接:https://blog.csdn.net/weixin_37928884/article/details/128407267

欄目分類
最近更新