3-7 ��󵲺c�}�C���B�z

MATLAB ¤]´£¨Ñ¤ñ¸û½ÆÂøªº¸ê®Æµ²ºc¡A¨Ò¦pµ²ºc°}¦C¡B²§½è°}¦Cµ¥¡C§Ú­Ì¤]¥i¥H¼¶¼gCµ{¦¡½X¡A¨Ó©MMATLAB¤º³¡ªºµ²ºc°}¦C¶i¦æ¸ê®Æªº¥æ´«©M­pºâ¡C ¦b¤U­±³o­Ó½d¨Ò matlabStruct2c.c¡A§Ú­Ì±N MATLAB ªºµ²ºc°}¦C¡A°e¨ì C µ{¦¡½X¤º³¡ªºµ²ºc°}¦C¡Aµ{¦¡½X¦p¤U¡G

­ì©lÀÉ¡]03-À³¥Îµ{¦¡¤¶­±/matlabStruct2c.c¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
// ¦¹½d¨Ò»¡©ú¦p¦ó¥Ñ MATLAB ªºµ²ºc°}¦CÂন C ªºµ²ºc°}¦C
// Roger Jang, 20050402

#include "mex.h"
#include <string.h>

#define	IN	prhs[0]		// ©w¸q¿é¥XÅܼÆ

typedef struct {
	char *name;
	double phone;
} PHONEBOOK;
PHONEBOOK *friend;	// C ªºµ²ºc°}¦C

void
mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[]) {
	int i, elementNum, strLen, status;

	// Àˬd¿é¤J©M¿é¥X°Ñ¼Æªº­Ó¼Æ
	if (nrhs<1) mexErrMsgTxt("One input argument required.");
	if (nlhs>0) mexErrMsgTxt("No output argument required.");

	elementNum = mxGetNumberOfElements(IN);		// ¤¸¯À­Ó¼Æ
	friend = (PHONEBOOK *)malloc(elementNum*sizeof(PHONEBOOK));	// ²£¥Í C ªºµ²ºc°}¦C
    
	// ¶ñ¤J C µ²ºc°}¦CªºÄæ¦ì­È
	for (i=0; i<elementNum; i++) {
		// ¶ñ¤J phone Äæ¦ì
		mxArray *fieldValue;
		fieldValue = mxGetField(IN, i, "phone");
		friend[i].phone = *mxGetPr(fieldValue);
		// ¶ñ¤J name Äæ¦ì
		fieldValue = mxGetField(IN, i, "name");
		strLen = (2*mxGetM(fieldValue)*mxGetN(fieldValue))+1;	// ­¼¤W2, ¥H¹ï¥I 2-byte ¤¤¤å¦r
		friend[i].name = (char *)malloc(strLen*sizeof(char));
		status = mxGetString(fieldValue, friend[i].name, strLen);
		if(status != 0) 
			mexWarnMsgTxt("Not enough space. String is truncated.");
	}

	// ¦C¦L C µ²ºc°}¦C
	for (i=0; i<elementNum; i++) {
		printf("friend[%d].name = %s\n", i, friend[i].name);
		printf("friend[%d].phone = %f\n", i, friend[i].phone);
	}

	// Free memory
	for (i=0; i<elementNum; i++)
		free(friend[i].name);
	free(friend);
}

¥i½sĶ¤Î´ú¸Õ¦p¤U¡G

Example 1: 03-À³¥Îµ{¦¡¤¶­±/matlabStruct2c01.mmex matlabStruct2c.c x(1).name = 'ªL¬F·½'; x(1).phone = 2796; x(2).name = '³¯¦¿§ø'; x(2).phone = 3425; x(3).name = '±i¥Ã´Ë'; x(3).phone = 3213; matlabStruct2c(x)friend[0].name = ªL¬F·½ friend[0].phone = 2796.000000 friend[1].name = ³¯¦¿§ø friend[1].phone = 3425.000000 friend[2].name = ±i¥Ã´Ë friend[2].phone = 3213.000000

¨ä¤¤¥Ñ MATLAB ¦L¥X¨Óªº°T®§¡A¨Æ¹ê¤W¤w¸g¬O C µ{¦¡½Xªºµ²ºc°}¦CªºÄæ¦ì­È¡C

¤U­±³o­Ó½d¨Ò cStruct2matlab.c¡A«h¬O¤Ï¨ä¹D¦Ó¦æ¡A¤]´N¬O±N C ªºµ²ºc°}¦C¡A°e¨ì MATLAB ¤u§@ªÅ¶¡ªºµ²ºc°}¦C¡A¦p¤U¡G

­ì©lÀÉ¡]03-À³¥Îµ{¦¡¤¶­±/cStruct2matlab.c¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
// ¦¹½d¨Ò»¡©ú¦p¦ó¥Ñ C ªºµ²ºc°}¦CÂন MATLAB ªºµ²ºc°}¦C
// Roger Jang, 20050402

#include "mex.h"
#include <string.h>

#define	OUT	plhs[0]		// ©w¸q¿é¥XÅܼÆ

struct phonebook {
	const char *name;
	double phone;
};
// C ªºµ²ºc°}¦C
struct phonebook friends[] = {{"§õ§»¾§", 3486},{"ªL¬F·½",3712}, {"³¯¦¿§ø", 2248}};
// ¥Î©ó MATLAB µ²ºc°}¦CªºÄæ¦ì¦WºÙ
const char *fieldNames[] = {"name", "phone"};

void
mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[]) {
	int structNum = sizeof(friends)/sizeof(struct phonebook);	// µ²ºc°}¦Cªºªø«×
	int fieldNum = sizeof(fieldNames)/sizeof(*fieldNames);		// Äæ¦ìªº­Ó¼Æ
	int dims[2] = {1, structNum};					// µ²ºc°}¦Cªººû«×
	int i, nameFieldIndex, phoneFieldIndex;

	// Àˬd¿é¤J©M¿é¥X°Ñ¼Æªº­Ó¼Æ
	if (nrhs>0) mexErrMsgTxt("No input argument required.");
	if (nlhs>1) mexErrMsgTxt("Too many output arguments.");
    
	// ²£¥Í¿é¥Xµ²ºc°}¦C
	OUT = mxCreateStructArray(2, dims, fieldNum, fieldNames);

	// ¨ú±oÄæ¦ì¦WºÙ¹ïÀ³ªº¯Á¤Þ­È¡A¥H«K¨Ï¥Î mxSetFieldByNumber() ¹ïÄæ¦ì­È¶i¦æ³]©w
	nameFieldIndex = mxGetFieldNumber(OUT, "name");
	phoneFieldIndex = mxGetFieldNumber(OUT, "phone");

	// ¶ñ¤J MATLAB µ²ºc°}¦CªºÄæ¦ì­È
	for (i=0; i<structNum; i++) {
		mxArray *fieldValue;
		// ¶ñ¤JÄæ¦ì¦WºÙ name ªº­È¡]¦³¨âºØ¤èªk¡^
	//	mxSetField(OUT, i, "name", mxCreateString(friends[i].name));			// ¤èªk¤@¡G®Ä²v¸û§C
		mxSetFieldByNumber(OUT, i, nameFieldIndex, mxCreateString(friends[i].name));	// ¤èªk¤G¡G®Ä²v¸û°ª
		// ¶ñ¤JÄæ¦ì¦WºÙ phone ªº­È¡]¦³¨âºØ¤èªk¡^
		fieldValue = mxCreateDoubleMatrix(1, 1, mxREAL);
		*mxGetPr(fieldValue) = friends[i].phone;
	//	mxSetField(OUT, i, "phone", fieldValue);					// ¤èªk¤@¡G®Ä²v¸û§C
		mxSetFieldByNumber(OUT, i, phoneFieldIndex, fieldValue);	// ¤èªk¤G¡G®Ä²v¸û°ª
	}
}

¥i½sĶ¤Î´ú¸Õ¦p¤U¡G

Example 2: 03-À³¥Îµ{¦¡¤¶­±/cStruct2matlab01.mmex cStruct2matlab.c x = cStruct2matlabx = 1x3 struct array with fields: name phone

¨ä¤¤ MATLAB ¦b¤u§@ªÅ¶¡ªºµ²ºc°}¦C x §Y¬O¥Ñ C ªºµ²ºc°}¦CÂà´«¥X¨Óªºµ²ªG¡C
MATLABµ{¦¡³]­p¡G¶i¶¥½g