3-7 對於結????????

MATLAB ]ѤƵcAҦpc}CB}CCڭ̤]iHgC{XAөMMATLABc}Ciƪ洫MpC bUoӽd matlabStruct2c.cAڭ̱N MATLAB c}CAe C {Xc}CA{XpUG

lɡ]03-ε{/matlabStruct2c.c^G]ǦϰUYi^
// dһp MATLAB c}Cন C c}C
// Roger Jang, 20050402

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

#define	IN	prhs[0]		// wqXܼ

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;

	// ˬdJMXѼƪӼ
	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, HI 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.");
	}

	// CL 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);
}

isĶδզpUG

Example 1: 03-ε{/matlabStruct2c01.mmex matlabStruct2c.c x(1).name = 'LF'; x(1).phone = 2796; x(2).name = ''; x(2).phone = 3425; x(3).name = 'iô'; x(3).phone = 3213; matlabStruct2c(x)friend[0].name = LF friend[0].phone = 2796.000000 friend[1].name = friend[1].phone = 3425.000000 friend[2].name = iô friend[2].phone = 3213.000000

䤤 MATLAB LXӪTAƹWwgO C {Xc}CȡC

Uoӽd cStruct2matlab.cAhOϨDӦA]NON C c}CAe MATLAB u@Ŷc}CApUG

lɡ]03-ε{/cStruct2matlab.c^G]ǦϰUYi^
// dһp C c}Cন MATLAB c}C
// Roger Jang, 20050402

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

#define	OUT	plhs[0]		// wqXܼ

struct phonebook {
	const char *name;
	double phone;
};
// C c}C
struct phonebook friends[] = {{"", 3486},{"LF",3712}, {"", 2248}};
// Ω MATLAB c}CW
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;

	// ˬdJMXѼƪӼ
	if (nrhs>0) mexErrMsgTxt("No input argument required.");
	if (nlhs>1) mexErrMsgTxt("Too many output arguments.");
    
	// ͿXc}C
	OUT = mxCreateStructArray(2, dims, fieldNum, fieldNames);

	// oWٹޭȡAHKϥ mxSetFieldByNumber() ȶi]w
	nameFieldIndex = mxGetFieldNumber(OUT, "name");
	phoneFieldIndex = mxGetFieldNumber(OUT, "phone");

	// J MATLAB c}C
	for (i=0; i<structNum; i++) {
		mxArray *fieldValue;
		// JW name ȡ]ؤk^
	//	mxSetField(OUT, i, "name", mxCreateString(friends[i].name));			// k@GIJvC
		mxSetFieldByNumber(OUT, i, nameFieldIndex, mxCreateString(friends[i].name));	// kGGIJv
		// JW phone ȡ]ؤk^
		fieldValue = mxCreateDoubleMatrix(1, 1, mxREAL);
		*mxGetPr(fieldValue) = friends[i].phone;
	//	mxSetField(OUT, i, "phone", fieldValue);					// k@GIJvC
		mxSetFieldByNumber(OUT, i, phoneFieldIndex, fieldValue);	// kGGIJv
	}
}

isĶδզpUG

Example 2: 03-ε{/cStruct2matlab01.mmex cStruct2matlab.c x = cStruct2matlabx = 1x3 struct array with fields: name phone

䤤 MATLAB bu@Ŷc}C x YO C c}CഫXӪGC
MATLAB{]pGig