Updated copyright headers
[libdai.git] / src / matlab / dai_writefg.cpp
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
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.
6 *
7 * Copyright (C) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
8 * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
9 */
10
11
12 #include <iostream>
13 #include "mex.h"
14 #include <dai/matlab/matlab.h>
15 #include <dai/factorgraph.h>
16
17
18 using namespace std;
19 using namespace dai;
20
21
22 /* Input Arguments */
23
24 #define PSI_IN prhs[0]
25 #define FILENAME_IN prhs[1]
26 #define NR_IN 2
27
28
29 /* Output Arguments */
30
31 #define NR_OUT 0
32
33
34 void mexFunction( int nlhs, mxArray * /*plhs*/[], int nrhs, const mxArray*prhs[] ) {
35 char *filename;
36
37 // Check for proper number of arguments
38 if ((nrhs != NR_IN) || (nlhs != NR_OUT)) {
39 mexErrMsgTxt("Usage: dai_writefg(psi,filename);\n\n"
40 "\n"
41 "INPUT: psi = linear cell array containing the factors\n"
42 " (psi{i} should be a structure with a Member field\n"
43 " and a P field, like a CPTAB).\n"
44 " filename = filename of a .fg file\n");
45 }
46
47 // Get input parameters
48 vector<Factor> factors = mx2Factors(PSI_IN,0);
49
50 size_t buflen;
51 buflen = mxGetN( FILENAME_IN ) + 1;
52 filename = (char *)mxCalloc( buflen, sizeof(char) );
53 mxGetString( FILENAME_IN, filename, buflen );
54
55 // Construct factorgraph
56 FactorGraph fg(factors);
57
58 try {
59 fg.WriteToFile( filename );
60 } catch( std::exception &e ) {
61 mexErrMsgTxt( e.what() );
62 }
63
64 return;
65 }