⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 engprog.c

📁 这是一个MATLAB与C直接如何进行相互转换的技术资料
💻 C
字号:
/* $Revision: 1.6 $ */
/*
 *	engprog.c 
 *
 *	This is a simple program that illustrates how to call the MATLAB
 *	Engine functions from a C program.
 *
 * Copyright Nitin S, TEL
 * All rights reserved
 */

/*	Include the required MATLAB libraries.
	If MATLAB libraries path is not set in VC++ compiler please set it to
	C:\MATLAB6p1\extern\lib\win32\microsoft\msvc60 
*/

#pragma comment(lib, "libeng.lib")
#pragma comment(lib, "libmx.lib")
#pragma comment(lib, "libmatlb.lib")
#pragma comment(lib, "libmat.lib")
#pragma comment(lib, "libmmfile.lib")

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "engine.h"


int main()

{
	int i ;
	Engine *ep; // Create a pointer to MATLAB engine

	mxArray *x_mdata = NULL, *y_mdata = NULL;

	double datax[20] ;
	double datay[20] ;
	
	/*
	 * Start the MATLAB engine locally by executing the string
	 * "matlab"
	 *
	 * To start the session on a remote host, use the name of
	 * the host as the string rather than \0	
	 */

	if (!(ep = engOpen("\0"))) {
		fprintf(stderr, "\nCan't start MATLAB engine\n");
		return 0;
	}

	
	/* 
	 * Create a variable for sending data to MATLAB workspace
	 */
	x_mdata = mxCreateDoubleMatrix(1, 20, mxREAL);
	y_mdata = mxCreateDoubleMatrix(1, 20, mxREAL);

	/*Assign names for variables. this is the name in MATLAB workspace*/
	mxSetName(x_mdata, "xvalue");
	mxSetName(y_mdata, "yvalue");

	for(i = 0; i<20; i++)
	{
		datax[i] = 3* (180/3.14)*cos(i) ;
		datay[i] = 3* (180/3.14)*sin(i) ;
	}

	memcpy((void *)mxGetPr(x_mdata), (void *)datax, sizeof(datax));
	memcpy((void *)mxGetPr(y_mdata), (void *)datay, sizeof(datay));
	/*
	 * Place the variables into the MATLAB workspace. Check the Workspace after this command
	 you can see the variables of name xvalue and yvalue there
	 */
	engPutArray(ep, x_mdata);
	engPutArray(ep, y_mdata);

	//Evaluate the expression in MATLAB workspace now.
	engEvalString(ep, "plot(xvalue, yvalue);");
	
	 
	/*
	 * We're done! Free memory, close MATLAB engine and exit.
	 */
	printf("Done!\n");
	mxDestroyArray(x_mdata);
	mxDestroyArray(y_mdata);
	//engClose(ep);
	
	return 0;
}







⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -