New git HEAD version
[libdai.git] / src / matlab / dai_writefg.cpp
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4 *
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6 */
7
8
9 #include <iostream>
10 #include "mex.h"
11 #include <dai/matlab/matlab.h>
12 #include <dai/factorgraph.h>
13
14
15 using namespace std;
16 using namespace dai;
17
18
19 /* Input Arguments */
20
21 #define PSI_IN prhs[0]
22 #define FILENAME_IN prhs[1]
23 #define NR_IN 2
24
25
26 /* Output Arguments */
27
28 #define NR_OUT 0
29
30
31 void mexFunction( int nlhs, mxArray * /*plhs*/[], int nrhs, const mxArray*prhs[] ) {
32 char *filename;
33
34 // Check for proper number of arguments
35 if ((nrhs != NR_IN) || (nlhs != NR_OUT)) {
36 mexErrMsgTxt("Usage: dai_writefg(psi,filename);\n\n"
37 "\n"
38 "INPUT: psi = linear cell array containing the factors\n"
39 " (psi{i} should be a structure with a Member field\n"
40 " and a P field, like a CPTAB).\n"
41 " filename = filename of a .fg file\n");
42 }
43
44 // Get input parameters
45 vector<Factor> factors = mx2Factors(PSI_IN,0);
46
47 size_t buflen;
48 buflen = mxGetN( FILENAME_IN ) + 1;
49 filename = (char *)mxCalloc( buflen, sizeof(char) );
50 mxGetString( FILENAME_IN, filename, buflen );
51
52 // Construct factorgraph
53 FactorGraph fg(factors);
54
55 try {
56 fg.WriteToFile( filename );
57 } catch( std::exception &e ) {
58 mexErrMsgTxt( e.what() );
59 }
60
61 return;
62 }