How do I access the arguments passed in mexFunction in MATLAB

10 views (last 30 days)
How do I access the arguments passed in mexFunction?
I thought if i did prhs[0] i would acces the first input argument that were given.
#include "mex.h"
#include "matrix.h"
void
mexFunction(int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[])
{
mexPrintf("%d \n", prhs[0]);
}
All I get is many numbers which i think is from the memory so I got this wrong.
Even tried :
#include <stdio.h>
#include <string.h>
#include "mex.h"
void
mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]) {
mxArray *array_ptr;
mexPrintf("%d /n", array_ptr);
}
With this i think I got the right pointer adress? But I don´t get if I have the right pointer adress for example how do I print out the value then?
I read about mxGetPr and mxGetData already. I tried void *mxGetData(const mxArray *pm); in the code. This should give me a pointer to the value? But I don´t now how to acces this pointer value then.
I read some of the great example code but I just don´t get it. I would be very glad if someone coulg help me or point me in the right direction.

Answers (1)

Geoff Hayes
Geoff Hayes on 6 Oct 2014
Edited: Geoff Hayes on 9 Oct 2014
Anton - look closely at the function signature, and in particular the last input parameter
const mxArray * prhs[]
So prhs is an array of pointers to mxArray types. So trying
mexPrintf("%d \n", prhs[0]);
will write out the address of the mxArray that the first pointer in the array is pointing to.
If you want to access the values, then you could do something like the following, assuming that the first input parameter is a double
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
double* A = 0;
size_t m = 0;
size_t n = 0;
int u = 0;
int v = 0;
// initialize the double pointer
A = mxGetPr(prhs[0]);
// get the dimensions of the first parameter
m = mxGetM(prhs[0]);
n = mxGetN(prhs[0]);
// write out the input
for (u=0;u<m;++u)
{
for (v=0;v<n;++v)
{
mexPrintf("%f ", A[u + v*m]);
}
mexPrintf("\n");
}
}
The above code assumes just one input parameter, which could be a scalar, array, or matrix, of type double. The A pointer is initialized with the address of the first element of the input (double) data via the mxGetPr function. We determine the dimension of this input, obtaining the number of rows with mxGetM, and the number of columns with mxGetN. We then write out the data.
Save the above function in a file named writeInput.c and compile as
mex writeInput.c
Test with the following examples
writeInput(1)
writeInput(1:4)
writeInput((1:4)')
writeInput(eye(4))
writeInput(eye(3,4))
writeInput(magic(6))
Using the above as a guide, it is relatively easy to handle multiple inputs of different data types.
EDIT
Changed above code so that it could compile with 'lcc-win32', 'Microsoft Windows SDK 7.1 (C)' and 'Xcode with Clang'. The difference is that all local variables must be declared together at the start of the function, rather than declaring (for example) the looping integers in the for block.
  9 Comments
Anton
Anton on 9 Oct 2014
Edited: Anton on 9 Oct 2014
Yes of course, forgot that. I run 64bit windows but that shouldn´t matter I guess?
Geoff Hayes
Geoff Hayes on 9 Oct 2014
Anton - you didn't remove the int declarations for the u and v variables in the for loops. Your code is
int u = 0;
int v = 0;
// etc.
// write out the input
for (int u=0;u<m;++u) <--- note the int
{
for (int v=0;v<n;++v) <--- note the int
Please change the for loops to
// write out the input
for (u=0;u<m;++u)
{
for (v=0;v<n;++v)
since u and v have been declared already as integers. Once you have made the changes, save the file, and build again.

Sign in to comment.

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!