How do I access the arguments passed in mexFunction in MATLAB

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)

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

Apparently there is many errors in your code. Tried to compile it with mex filename.c but it get a lot of errors. I did not understand what you did different. Do you really have to do m = mxGetM(prhs[0]); n = mxGetN(prhs[0]); to check matrix size even tho I only want to use one argument? In c it would have worked to just do argv[1] to write out the first argument
int main(int argc, char * argv[]){
puts(argv[1])
}
Why is it so differet here? Don´t understand. How do const mxArray *prhs[] differ from mxArray *prhs[], I guess that mxArray *prhs[] should be similar to char * argv[] in c? But the mxArray is some kind of strange type of array?
There are no errors in the code. It is compiled with R2014a, and Clang on OS X 10.8.5.
Tried to compile it with mex filename.c but it get a lot of errors.
What are the errors? What version of MATLAB are you using, what OS, and which compiler? Did you copy the above code directly or make some changes or...?
Do you really have to do m = mxGetM(prhs[0]); n = mxGetN(prhs[0]); to check matrix size even tho I only want to use one argument?
No, of course not. This code is designed as an example of what you can do. If your code is only expecting a scalar input, then it can be written with that in mind.
In c it would have worked to just do argv[1] to write out the first argument....Why is it so differet here?
For the main function in a C program, yes, that would work. But why should this behave identically to a C program? Especially as scalars, arrays, matrices, structs, cell, can all be passed as input arguments into the MEX functions.
How do const mxArray *prhs[] differ from mxArray *prhs[]
Inputs to the function are constant with const mxArray* prhs[]. Please review what is the difference between int*, const int* etc..
But the mxArray is some kind of strange type of array?
It is an opaque type. as the documentation at mxArray will discuss.
For more information on MEX files, consider reading the documentation at Components of MEX-File.
mex writeinput.c
Building with 'Microsoft Windows SDK 7.1 (C)'.
Error using mex
writeinput.c
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(17) : error C2143: syntax error : missing ';' before 'type'
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(17) : error C2143: syntax error : missing ';' before 'type'
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(17) : error C2143: syntax error : missing ')' before 'type'
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(17) : error C2143: syntax error : missing ';' before 'type'
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(17) : error C2065: 'u' : undeclared identifier
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(17) : warning C4552: '<' : operator has no effect; expected operator with
side-effect
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(17) : error C2059: syntax error : ')'
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(18) : error C2065: 'u' : undeclared identifier
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(18) : error C2143: syntax error : missing ';' before '{'
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(19) : error C2143: syntax error : missing ';' before 'type'
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(19) : error C2143: syntax error : missing ';' before 'type'
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(19) : error C2143: syntax error : missing ')' before 'type'
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(19) : error C2143: syntax error : missing ';' before 'type'
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(19) : error C2065: 'v' : undeclared identifier
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(19) : warning C4552: '<' : operator has no effect; expected operator with
side-effect
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(19) : error C2059: syntax error : ')'
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(20) : error C2065: 'v' : undeclared identifier
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(20) : error C2143: syntax error : missing ';' before '{'
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(21) : error C2065: 'u' : undeclared identifier
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(21) : error C2065: 'v' : undeclared identifier
I use windows 8.1, MATLAB R2014A, Microsoft Windows SDK 7.1 I used your code only as it is. It would be nice to get this to work so I can test how inputs work.
Some compilers behave differently. As most of the errors point to the u and v loop variables, that is a clue that something may not be quite right. Try declaring them as integers outside of the for loops, in the block of code that declares all local variables to this function. That would mean changing the contents of the file to
#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");
}
}
mex writeinput.c
Building with 'Microsoft Windows SDK 7.1 (C)'.
Error using mex
writeinput.c
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(19) : error C2143: syntax error : missing ';' before 'type'
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(19) : error C2143: syntax error : missing ';' before 'type'
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(19) : error C2143: syntax error : missing ')' before 'type'
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(19) : error C2143: syntax error : missing ';' before 'type'
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(19) : warning C4552: '<' : operator has no effect; expected operator with
side-effect
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(19) : error C2059: syntax error : ')'
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(20) : error C2143: syntax error : missing ';' before '{'
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(21) : error C2143: syntax error : missing ';' before 'type'
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(21) : error C2143: syntax error : missing ';' before 'type'
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(21) : error C2143: syntax error : missing ')' before 'type'
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(21) : error C2143: syntax error : missing ';' before 'type'
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(21) : warning C4552: '<' : operator has no effect; expected operator with
side-effect
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(21) : error C2059: syntax error : ')'
C:\Users\Snigelfarfar\Documents\MATLAB\writeinput.c(22) : error C2143: syntax error : missing ';' before '{'
Anton - attach the file that you are using to build the mex function, as I have no idea what you are compiling.
After suggesting to Anton that he make changes to the code as described in a previous comment (declaring the u and v with the other local variables at the beginning of the function), I tested the code again with
mex writeInput.c
and the code could compile and the function could be executed for
  • Clang (Apple LLVM version 4.2 (clang-425.0.28) OS X 10.8.5)
  • lcc-win32 (Vista)
  • Microsoft Windows SDK 7.1 (C) (32-bit Windows Vista)
Yes of course, forgot that. I run 64bit windows but that shouldn´t matter I guess?
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

Asked:

on 6 Oct 2014

Commented:

on 9 Oct 2014

Community Treasure Hunt

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

Start Hunting!