00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "mex.h"
00018 #include <string.h>
00019
00020 #define NUMBER_OF_STRUCTS (sizeof(friends)/sizeof(struct phonebook))
00021 #define NUMBER_OF_FIELDS (sizeof(field_names)/sizeof(*field_names))
00022
00023 struct phonebook
00024 {
00025 const char *name;
00026 double phone;
00027 };
00028
00029 void
00030 mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
00031 {
00032 const char *field_names[] = {"name", "phone"};
00033 struct phonebook friends[] = {{"Jordan Robert", 3386},{"Mary Smith",3912},
00034 {"Stacy Flora", 3238},{"Harry Alpert",3077}};
00035 mwSize dims[2] = {1, NUMBER_OF_STRUCTS };
00036 int name_field, phone_field;
00037 mwIndex i;
00038
00039 (void) prhs;
00040
00041
00042 if (nrhs !=0) {
00043 mexErrMsgTxt("No input argument required.");
00044 }
00045 if(nlhs > 1){
00046 mexErrMsgTxt("Too many output arguments.");
00047 }
00048
00049
00050 plhs[0] = mxCreateStructArray(2, dims, NUMBER_OF_FIELDS, field_names);
00051
00052
00053
00054
00055
00056 name_field = mxGetFieldNumber(plhs[0],"name");
00057 phone_field = mxGetFieldNumber(plhs[0],"phone");
00058
00059
00060 for (i=0; i<NUMBER_OF_STRUCTS; i++) {
00061 mxArray *field_value;
00062
00063
00064 mxSetFieldByNumber(plhs[0],i,name_field,mxCreateString(friends[i].name));
00065 field_value = mxCreateDoubleMatrix(1,1,mxREAL);
00066 *mxGetPr(field_value) = friends[i].phone;
00067
00068
00069 mxSetFieldByNumber(plhs[0],i,phone_field,field_value);
00070 }
00071 }
00072