Octave logo Octave-Forge - Extra packages for GNU Octave
Home · Packages · Developers · Documentation · Function Reference · FAQ · Bugs · Mailing Lists · Links · SVN
  • Main Page
  • Classes
  • Files

examples/mystruct.c

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2006, 2007 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or 
00008 modify it under the terms of the GNU General Public License 
00009 as published by the Free Software Foundation; either
00010 version 3  of the License, or (at your option) any later 
00011 version.
00012 
00013 Octave is distributed in the hope that it will be useful, 
00014 but WITHOUT ANY WARRANTY; without even the implied warranty
00015 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00016 See the GNU General Public License for more details.
00017 
00018 You should have received a copy of the GNU General Public 
00019 License along with Octave; see the file COPYING.  If not,
00020 see <http://www.gnu.org/licenses/>.
00021 
00022 */
00023 
00024 #include "mex.h"
00025 
00026 void
00027 mexFunction (int nlhs, mxArray* plhs[], int nrhs, 
00028              const mxArray* prhs[])
00029 {
00030   int i;
00031   mwIndex j;
00032   mxArray *v;
00033   const char *keys[] = { "this", "that" };
00034 
00035   if (nrhs != 1 || ! mxIsStruct (prhs[0]))
00036     mexErrMsgTxt ("expects struct");
00037 
00038   for (i = 0; i < mxGetNumberOfFields (prhs[0]); i++)
00039     for (j = 0; j < mxGetNumberOfElements (prhs[0]); j++)
00040       {
00041         mexPrintf ("field %s(%d) = ", 
00042                    mxGetFieldNameByNumber (prhs[0], i), j);
00043         v = mxGetFieldByNumber (prhs[0], j, i);
00044         mexCallMATLAB (0, 0, 1, &v, "disp");
00045       }
00046 
00047   v = mxCreateStructMatrix (2, 2, 2, keys);
00048 
00049   mxSetFieldByNumber (v, 0, 0, mxCreateString ("this1"));
00050   mxSetFieldByNumber (v, 0, 1, mxCreateString ("that1"));
00051   mxSetFieldByNumber (v, 1, 0, mxCreateString ("this2"));
00052   mxSetFieldByNumber (v, 1, 1, mxCreateString ("that2"));
00053   mxSetFieldByNumber (v, 2, 0, mxCreateString ("this3"));
00054   mxSetFieldByNumber (v, 2, 1, mxCreateString ("that3"));
00055   mxSetFieldByNumber (v, 3, 0, mxCreateString ("this4"));
00056   mxSetFieldByNumber (v, 3, 1, mxCreateString ("that4"));
00057 
00058   if (nlhs)
00059     plhs[0] = v;
00060 }
SourceForge.net Logo