Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleMEX.cpp

This is a small example of a library that can be called from MATLAB (C) to execute a simple matrix-multipl. using the BIAS-libraries. It takes two matrices as arguments and returns the result of the multiplication.When this library has been compiled, copy it to your MATLAB-workspace and rename it to: SomeFuncName.mexglx (Linux) or SomeFuncName.dll (WIN32). Afterwards you can call the mex-file from within MATLAB using: C = SomeFuncName(A,B) (A,B initialized with matrices, C is used to store the result)

Author
apetersen
Date
07/04/2008
/* This file is part of the BIAS library (Basic ImageAlgorithmS).
Copyright (C) 2003, 2004 (see file CONTACTS for details)
Multimediale Systeme der Informationsverarbeitung
Institut fuer Informatik
Christian-Albrechts-Universitaet Kiel
BIAS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
BIAS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with BIAS; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/
/**
@example ExampleMEX.cpp
@relates MatLabInterface
@brief This is a small example of a library that can be called from MATLAB (C)
to execute a simple matrix-multipl. using the BIAS-libraries. It takes two
matrices as arguments and returns the result of the multiplication.
When this library has been compiled, copy it to your MATLAB-workspace and
rename it to: SomeFuncName.mexglx (Linux) or SomeFuncName.dll (WIN32).
Afterwards you can call the mex-file from within MATLAB using:
C = SomeFuncName(A,B)
(A,B initialized with matrices, C is used to store the result)
@ingroup g_examples
@author apetersen
@date 07/04/2008
*/
#include <Base/Math/MatLabInterface.hh>
using namespace BIAS;
void mexFunction(//number of left-hand (output) arguments
int nlhs,
//pointer to the left-hand (output) arguments
mxArray *plhs[],
//number of right-hand (input) arguments
int nrhs,
//pointer to the right-hand (input) arguments
const mxArray *prhs[])
{
//check number of input params
if (nrhs != 2 || nlhs != 1)
MATLABERR("Invalid argument count!");
//convert MATLAB-data to BIAS-matrices
Matrix<double> mat1, mat2, result;
if (MxArrToBIASMatrix(prhs[0], mat1) != 0) {
MATLABERR("First argument (source) is invalid!");
return;
}
if (MxArrToBIASMatrix(prhs[1], mat2) != 0) {
MATLABERR("Second argument (kernel) is invalid!");
return;
}
//check if multipication is possible
if (mat1.GetCols() != mat2.GetRows()) {
MATLABERR("Error: invalid matrix dimensions!");
return;
}
//output matrix to MATLAB-screen instead of cout/cerr!
for (int i=0; i<3; i++) {
for (int j=0; j<3; j++) {
mexPrintf("mat1[%d][%d]: %f ; ", i, j, mat1[i][j]);
}
mexPrintf("\n");
}
//do multiplication
result = mat1 * mat2;
//convert result to MATLAB-data
if (BIASMatrixToMxArr(result, plhs[0]) != 0) {
MATLABERR("Error converting result to MATLAB-datatypes!");
return;
}
return;
}