SWZ: C functions to call from WZ code to return values in Matlab (either in a global structure, or in a mat file)
parent
3ef85c55c3
commit
8d46da3801
|
@ -0,0 +1,77 @@
|
|||
/*
|
||||
* Copyright (C) 2011 Dynare Team
|
||||
*
|
||||
* This file is part of Dynare.
|
||||
*
|
||||
* Dynare is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Dynare is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Dynare. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "modify_for_mex.h"
|
||||
|
||||
void
|
||||
mex_write_to_matlab_matfile(double *data, int rows, int cols, const char *varname, const char *filename)
|
||||
{
|
||||
mxArray *toWrite;
|
||||
toWrite = getMxArray(data, rows, cols);
|
||||
MATFile *matfile = matOpen(filename, "w");
|
||||
if (matfile == NULL)
|
||||
mexErrMsgTxt("Error encountered in mex when opening a .mat file");
|
||||
|
||||
if (matPutVariable(matfile, varname, toWrite) != 0 ||
|
||||
ferror(matGetFp(matfile)) > 0 ||
|
||||
feof(matGetFp(matfile)) > 0)
|
||||
mexErrMsgTxt("Error encountered in mex when writing a .mat file");
|
||||
|
||||
if (matClose(matfile) != 0)
|
||||
mexErrMsgTxt("Error encountered in mex when closing a .mat file");
|
||||
}
|
||||
|
||||
void
|
||||
mex_write_to_matlab_global_struct(double *data, int rows, int cols, const char *varname)
|
||||
{
|
||||
mxArray *toWrite;
|
||||
toWrite = getMxArray(data, rows, cols);
|
||||
|
||||
int fieldnumber = mxAddField(globalMatlabStruct, varname);
|
||||
if (fieldnumber < 0)
|
||||
mexErrMsgTxt("Error encountered in mex when assigining to global structure");
|
||||
mxSetFieldByNumber(globalMatlabStruct, 0, fieldnumber, toWrite);
|
||||
}
|
||||
|
||||
mxArray *
|
||||
getMxArray(double *data, int rows, int cols)
|
||||
{
|
||||
mxArray *mat;
|
||||
double *pind;
|
||||
int i;
|
||||
|
||||
if (rows == 1 && cols == 1)
|
||||
mat = mxCreateDoubleScalar(data[0]);
|
||||
else
|
||||
{
|
||||
mat = mxCreateDoubleMatrix(rows, cols, mxREAL);
|
||||
if (mat != NULL)
|
||||
{
|
||||
pind = mxGetPr(mat);
|
||||
for (i = 0; i < rows*cols; i++)
|
||||
pind[i] = data[i];
|
||||
}
|
||||
}
|
||||
if (mat == NULL)
|
||||
mexErrMsgTxt("Error encountered in mex: not enough heap space to allocate memory for mxArray");
|
||||
return mat;
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (C) 2010 Dynare Team
|
||||
* Copyright (C) 2010-2011 Dynare Team
|
||||
*
|
||||
* This file is part of Dynare.
|
||||
*
|
||||
|
@ -19,34 +19,43 @@
|
|||
|
||||
#ifndef _MEXMOD
|
||||
#define _MEXMOD
|
||||
|
||||
void swz_exit(int status);
|
||||
void swz_fprintf_err(const char * str, ...);
|
||||
extern int constant_seed;
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(MATLAB_MEX_FILE) || defined(OCTAVE_MEX_FILE)
|
||||
|
||||
|
||||
#include <dynmex.h>
|
||||
#include <dynblas.h>
|
||||
#include <dynlapack.h>
|
||||
#include <mat.h>
|
||||
|
||||
#define swzMalloc mxMalloc
|
||||
#define swzCalloc mxCalloc
|
||||
#define swzRealloc mxRealloc
|
||||
#define swzFree mxFree
|
||||
|
||||
mxArray *globalMatlabStruct;
|
||||
|
||||
#ifndef _MEXWRITE
|
||||
#define _MEXWRITE
|
||||
|
||||
void mex_write_to_matlab_matfile(double *, int, int, const char *, const char *);
|
||||
void mex_write_to_matlab_global_struct(double *, int, int, const char *);
|
||||
mxArray *getMxArray(double *, int, int);
|
||||
|
||||
#endif
|
||||
|
||||
#else
|
||||
|
||||
|
||||
#define swz_fprintf_stdout printf
|
||||
#define swzMalloc malloc
|
||||
#define swzCalloc calloc
|
||||
#define swzRealloc realloc
|
||||
#define swzFree free
|
||||
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue