8ae625f6f22b777b89faece8025efb6644ff2670
[libdai.git] / matlab / dai_readfg.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 * [psi] = dai_readfg(filename); *
27 * *
28 *=================================================================*/
29
30
31 #include <iostream>
32 #include "mex.h"
33 #include "matlab.h"
34 #include "factorgraph.h"
35
36
37 using namespace std;
38
39
40 /* Input Arguments */
41
42 #define FILENAME_IN prhs[0]
43 #define NR_IN 1
44
45
46 /* Output Arguments */
47
48 #define PSI_OUT plhs[0]
49 #define NR_OUT 1
50
51
52 void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[] )
53 {
54 char *filename;
55
56
57 // Check for proper number of arguments
58 if ((nrhs != NR_IN) || (nlhs != NR_OUT)) {
59 mexErrMsgTxt("Usage: [psi] = dai_readfg(filename);\n\n"
60 "\n"
61 "INPUT: filename = filename of a .fg file\n"
62 "\n"
63 "OUTPUT: psi = linear cell array containing the factors\n"
64 " (psi{i} is a structure with a Member field\n"
65 " and a P field, like a CPTAB).\n");
66 }
67
68 // Get input parameters
69 size_t buflen;
70 buflen = mxGetN( FILENAME_IN ) + 1;
71 filename = (char *)mxCalloc( buflen, sizeof(char) );
72 mxGetString( FILENAME_IN, filename, buflen );
73
74
75 // Read factorgraph
76 FactorGraph fg;
77 if( fg.ReadFromFile( filename ) ) {
78 mexErrMsgTxt("dai_readfg: error reading file\n");
79 }
80
81
82 // Save factors
83 vector<Factor> psi;
84 for( size_t I = 0; I < fg.nrFactors(); I++ )
85 psi.push_back(fg.factor(I));
86
87
88 // Hand over results to MATLAB
89 PSI_OUT = Factors2mx(psi);
90
91
92 return;
93 }