core/matlab/examples/mx/mxcreatestructarray.c

00001 /*=================================================================
00002  * mxcreatestructarray.c
00003  *
00004  * mxcreatestructarray illustrates how to create a MATLAB structure
00005  * from a corresponding C structure.  It creates a 1-by-4 structure mxArray,
00006  * which contains two fields, name and phone number where name is store as a
00007  * string and phone number is stored as a double.  The structure that is
00008  * passed back to MATLAB could be used as input to the phonebook.c example
00009  * in $MATLAB/extern/examples/refbook.
00010  *
00011  * This is a MEX-file for MATLAB.  
00012  * Copyright 1984-2006 The MathWorks, Inc.
00013  * All rights reserved.
00014  *=================================================================*/
00015 
00016 /* $Revision: 1.1 $ */
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     /* Check for proper number of input and  output arguments */    
00042     if (nrhs !=0) {
00043         mexErrMsgTxt("No input argument required.");
00044     } 
00045     if(nlhs > 1){
00046         mexErrMsgTxt("Too many output arguments.");
00047     }
00048     
00049     /* Create a 1-by-n array of structs. */ 
00050     plhs[0] = mxCreateStructArray(2, dims, NUMBER_OF_FIELDS, field_names);
00051 
00052     /* This is redundant, but here for illustration.  Since we just
00053        created the structure and the field number indices are zero
00054        based, name_field will always be 0 and phone_field will always
00055        be 1 */
00056     name_field = mxGetFieldNumber(plhs[0],"name");
00057     phone_field = mxGetFieldNumber(plhs[0],"phone");
00058 
00059     /* Populate the name and phone fields of the phonebook structure. */ 
00060     for (i=0; i<NUMBER_OF_STRUCTS; i++) {
00061     mxArray *field_value;
00062     /* Use mxSetFieldByNumber instead of mxSetField for efficiency
00063        mxSetField(plhs[0],i,"name",mxCreateString(friends[i].name); */
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     /* Use mxSetFieldByNumber instead of mxSetField for efficiency
00068        mxSetField(plhs[0],i,"name",mxCreateString(friends[i].name); */
00069     mxSetFieldByNumber(plhs[0],i,phone_field,field_value);
00070     }
00071 }
00072 

GridLAB-DTM Version 1.0
An open-source project initiated by the US Department of Energy