Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleMEX.cpp
1 /* This file is part of the BIAS library (Basic ImageAlgorithmS).
2 
3  Copyright (C) 2003, 2004 (see file CONTACTS for details)
4  Multimediale Systeme der Informationsverarbeitung
5  Institut fuer Informatik
6  Christian-Albrechts-Universitaet Kiel
7 
8 
9  BIAS is free software; you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation; either version 2.1 of the License, or
12  (at your option) any later version.
13 
14  BIAS is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with BIAS; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/
22 
23 /**
24  @example ExampleMEX.cpp
25  @relates MatLabInterface
26  @brief This is a small example of a library that can be called from MATLAB (C)
27  to execute a simple matrix-multipl. using the BIAS-libraries. It takes two
28  matrices as arguments and returns the result of the multiplication.
29 
30  When this library has been compiled, copy it to your MATLAB-workspace and
31  rename it to: SomeFuncName.mexglx (Linux) or SomeFuncName.dll (WIN32).
32  Afterwards you can call the mex-file from within MATLAB using:
33  C = SomeFuncName(A,B)
34  (A,B initialized with matrices, C is used to store the result)
35  @ingroup g_examples
36  @author apetersen
37  @date 07/04/2008
38  */
39 
40 #include <Base/Math/MatLabInterface.hh>
41 
42 using namespace BIAS;
43 
44 void mexFunction(//number of left-hand (output) arguments
45  int nlhs,
46  //pointer to the left-hand (output) arguments
47  mxArray *plhs[],
48  //number of right-hand (input) arguments
49  int nrhs,
50  //pointer to the right-hand (input) arguments
51  const mxArray *prhs[])
52 {
53  //check number of input params
54  if (nrhs != 2 || nlhs != 1)
55  MATLABERR("Invalid argument count!");
56 
57  //convert MATLAB-data to BIAS-matrices
58  Matrix<double> mat1, mat2, result;
59  if (MxArrToBIASMatrix(prhs[0], mat1) != 0) {
60  MATLABERR("First argument (source) is invalid!");
61  return;
62  }
63  if (MxArrToBIASMatrix(prhs[1], mat2) != 0) {
64  MATLABERR("Second argument (kernel) is invalid!");
65  return;
66  }
67 
68  //check if multipication is possible
69  if (mat1.GetCols() != mat2.GetRows()) {
70  MATLABERR("Error: invalid matrix dimensions!");
71  return;
72  }
73 
74  //output matrix to MATLAB-screen instead of cout/cerr!
75  for (int i=0; i<3; i++) {
76  for (int j=0; j<3; j++) {
77  mexPrintf("mat1[%d][%d]: %f ; ", i, j, mat1[i][j]);
78  }
79  mexPrintf("\n");
80  }
81 
82  //do multiplication
83  result = mat1 * mat2;
84 
85  //convert result to MATLAB-data
86  if (BIASMatrixToMxArr(result, plhs[0]) != 0) {
87  MATLABERR("Error converting result to MATLAB-datatypes!");
88  return;
89  }
90 
91  return;
92 }
int MxArrToBIASMatrix(const mxArray *mxMat, BIAS::Matrix< double > &mat)
Convert MATLAB-array to BIAS::Matrix.
int BIASMatrixToMxArr(const BIAS::Matrix< double > &mat, mxArray *&mxMat)
Convert BIAS::Matrix to MATLAB-array, inclusive allocation!
unsigned int GetRows() const
Definition: Matrix.hh:202
unsigned int GetCols() const
Definition: Matrix.hh:204