Merged tests/*, matlab/*, utils/* from SVN head...
[libdai.git] / matlab / dai_writefg.cpp
1 /* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
2 Radboud University Nijmegen, The Netherlands
3
4 This file is part of libDAI.
5
6 libDAI is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 libDAI is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with libDAI; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21
22 /*=================================================================*
23 * *
24 * This is a MEX-file for MATLAB. *
25 * *
26 * dai_writefg(psi, filename); *
27 * *
28 *=================================================================*/
29
30
31 #include <iostream>
32 #include "mex.h"
33 #include <dai/matlab/matlab.h>
34 #include <dai/factorgraph.h>
35
36
37 using namespace std;
38 using namespace dai;
39
40
41 /* Input Arguments */
42
43 #define PSI_IN prhs[0]
44 #define FILENAME_IN prhs[1]
45 #define NR_IN 2
46
47
48 /* Output Arguments */
49
50 #define NR_OUT 0
51
52
53 void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[] )
54 {
55 char *filename;
56
57
58 // Check for proper number of arguments
59 if ((nrhs != NR_IN) || (nlhs != NR_OUT)) {
60 mexErrMsgTxt("Usage: dai_writefg(psi,filename);\n\n"
61 "\n"
62 "INPUT: psi = linear cell array containing the factors\n"
63 " (psi{i} should be a structure with a Member field\n"
64 " and a P field, like a CPTAB).\n"
65 " filename = filename of a .fg file\n");
66 }
67
68 // Get input parameters
69 vector<Factor> factors = mx2Factors(PSI_IN,0);
70
71 size_t buflen;
72 buflen = mxGetN( FILENAME_IN ) + 1;
73 filename = (char *)mxCalloc( buflen, sizeof(char) );
74 mxGetString( FILENAME_IN, filename, buflen );
75
76 // Construct factorgraph
77 FactorGraph fg(factors);
78 long nr_v = fg.nrVars();
79 long nr_f = fg.nrFactors();
80
81 try {
82 fg.WriteToFile( filename );
83 } catch( std::exception &e ) {
84 mexErrMsgTxt( e.what() );
85 }
86
87 return;
88 }