1 /* This file is part of libDAI - http://www.libdai.org/
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
7 * Copyright (C) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
8 * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
14 #include <dai/matlab/matlab.h>
15 #include <dai/factorgraph.h>
24 #define FILENAME_IN prhs[0]
28 /* Output Arguments */
30 #define PSI_OUT plhs[0]
34 void mexFunction( int nlhs
, mxArray
*plhs
[], int nrhs
, const mxArray
*prhs
[] ) {
38 // Check for proper number of arguments
39 if ((nrhs
!= NR_IN
) || (nlhs
!= NR_OUT
)) {
40 mexErrMsgTxt("Usage: [psi] = dai_readfg(filename);\n\n"
42 "INPUT: filename = filename of a .fg file\n"
44 "OUTPUT: psi = linear cell array containing the factors\n"
45 " (psi{i} is a structure with a Member field\n"
46 " and a P field, like a CPTAB).\n");
49 // Get input parameters
51 buflen
= mxGetN( FILENAME_IN
) + 1;
52 filename
= (char *)mxCalloc( buflen
, sizeof(char) );
53 mxGetString( FILENAME_IN
, filename
, buflen
);
59 fg
.ReadFromFile( filename
);
60 } catch( std::exception
&e
) {
61 mexErrMsgTxt( e
.what() );
67 for( size_t I
= 0; I
< fg
.nrFactors(); I
++ )
68 psi
.push_back(fg
.factor(I
));
71 // Hand over results to MATLAB
72 PSI_OUT
= Factors2mx(psi
);