How MATLAB mex files access MATLAB instances? - matlab

This is the entry point for every mex file:
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]);
In fact mex files are windows dll files with mexFunction as the main function.
My question is when the mex function is called, how it can access matlab instance specific data from inside the mex. As an example, consider 'mexPutVariable' function. It's job is 'copy an array from inside MEX-function into the specified workspace (outside mex)'. But how it knows where is 'workspace'. No parameter has passed to mex (like a pointer), containing matlab instance data (caller). mex files know only nlhs, plhs, nrhs, prhs and none of them can help mex files to excavate matlab instance specific data(caller function information).

A possible solution is that "Matlab.exe" is declaring mexPutVariable as an exported function:
[Matlab.exe]
int __declspec(dllexport) mexPutVariable(const char* workspace, const char* name, const mxArray* parray)
{
...
}
It is then very easy to retreive this function from the dll using GetModuleHandle and GetProcAddress:
[Module.dll]
// Declare function pointer
int (*FctnPtr)(const char* workspace, const char* name, const mxArray* parray);
// Retreive the main executable
HANDLE hExe = GetModuleHandle(NULL);
// Link to exported function in the exe just like you would do for any dll
FctnPtr mexPutVariable = (FctnPtr)GetProcAddress(hExe, "mexPutVariable");
// Use exported function from the dll
mexPutVariable("Base", "foo", myArray);
For compiling the mex file and after looking at mex.h file, mexPutVariable is declared as an external function to link with:
LIBMWMEX_API_EXTERN_C int mexPutVariable(const char* workspace, const char* name, const mxArray* parray);
Which turn to be simply (when compiled as dll side):
extern "C" int mexPutVariable(const char* workspace, const char* name, const mxArray* parray);

Related

Static function in Cpp file Doxygen

I generate the my source code with Doxygen,
Doxygen generate void and another fucntion but it didnt generate static function although EXTRACT_STATIC is YES.
I am currently using version of the Doxygen 1.8.19and
I added top of the example.cpp file /** #file example.cpp */
I am using .c and .cpp file together. In the example.cpp file I have:
/**
*#brief ..
*#param size The application receives in this parameter the size of the required memory block.
*#retval The application must return a pointer to the allocated and zeroed-out memory block.
*/
static void* CheckVal (unsigned int size)
{
void* result = malloc (size);
assert (result != NULL);
memset (result, 0, size);
return result;
}
I couldnt see any function in doxygen index.html . Why this happened?

How to declare double const* const* variable in cython?

I have a c++ function in "example.h":
bool myFunc(double const* const* p);
and I want to wrap it with cython code (in .pyx file).
Howerver, when I'm write the following code:
cdef extern from r"example.h":
bool myFunc(double const*const* p)
I'm receiving the following error:
Error compiling Cython file:
Expected ')', found '*'
and pycharm shows this error on double const* const* p:
Unresolved reference 'const'
How can I declare that kind of variables?
In C/C++, there is ongoing battle where to put the const-qualifier: either
void foo(const int *a);
or
void foo(int const *a);
both meaning the same thing.
There is no such battle in Cython, because it accept only the first version.
The above rule, applied to double** leads to:
cdef extern from r"example.h":
bool myFunc(const double * const* p)
Or as a work-around one could drop the const-qualifier altogether:
cdef extern from r"example.h":
bool myFunc(const double **p)
which I would not recommend, all above in large projects, where using const-qualifiers helps a lot when figuring out what happens.

C++ - Why do I have to include .cpp file along with/ instead of .h file to acces the value of a global variable in the following case?

I am trying to properly declare and define global variables in separate files and include them in a third file which deals with class declaration.
The three files are:
1) global.h
#ifndef GLOBAL_H_INCLUDED
#define GLOBAL_H_INCLUDED
extern const int marker_num;
extern const int dim;
using namespace std;
#endif // GLOBAL_H_INCLUDED
2) global.cpp
#include <iostream>
#include <cstdio>
#include <cmath>
#include "global.h"
#include "WorldState.h"
#include "Robot.h"
#include "Sensor.h"
#include "Marker.h"
constexpr const int marker_num = 10;
constexpr const int dim = (2 * marker_num) + 3;
3) WorldState.h
#ifndef WORLDSTATE_H
#define WORLDSTATE_H
#include "global.h"
#include "global.cpp"
class WorldState{
public:
WorldState(float a[], float b[dim][dim]);
get_wstate();
protected:
private:
float w_state[];
float covar_matrix[dim][dim];
};
#endif // WORLDSTATE_H
I am using the global variable dim to declare and define a multidimensional array. I have declared dim inside global.h and defined it inside global.cpp. Now, I have a class called WorldState and inside its header, I am using dim. If I comment out #include "global.cpp", it throws the following error:
C:\Users\syamp\Documents\codeblocks\slam\WorldState.h|10|error: array bound is not an integer constant before ']' token
My understanding is that including the .h file includes the corresponding .cpp as well, and that all declarations should be inside .h and all definitions should be inside .cpp. However, it doesn't seem to work in this case.
1) If I decide to include global.cpp file inside WorldState.h, isn't it bad programming practice? I am trying to write a good code not just a code that works.
2) An alternative is to define values of variable(s) dim (and marker_num) inside global.h. Is that good programming practice?
3) I believe there is something that I am missing. Kindly suggest the best method to resolve this issue. I am using codeblocks and C++11. Thanks in advance.
I am using the global variable dim to declare and define a multidimensional array.
When declaring a fixed-length array at compile-time, the value(s) of its dimension(s) must be known to the compiler, but your separation prevents the value of dim from being known to the compiler at all, so dim cannot be used to specify fixed array dimensions. Any code that uses dim will just compile into a reference to it, and then the linker will resolve the references after compilation is done. Just because dim is declared as const does not make it suitable as a compile-time constant. To do that, you must define its value in its declaration, eg:
#ifndef GLOBAL_H_INCLUDED
#define GLOBAL_H_INCLUDED
static constexpr const int marker_num = 10;
static constexpr const int dim = (2 * marker_num);
using namespace std;
#endif // GLOBAL_H_INCLUDED
Otherwise, if you keep dim's declaration and definition in separate files, you will have to dynamically allocate the array at run-time instead of statically at compile-time.
I have declared dim inside global.h and defined it inside global.cpp.
That is fine for values you don't need to use until run-time. That will not work for values you need to use at compile-time.
My understanding is that including the .h file includes the corresponding .cpp as well
That is not even remotely true. The project/makefile brings in the .cpp file when invoking the compiler. The .h file has nothing to do with that.
that all declarations should be inside .h and all definitions should be inside .cpp.
Typically yes, but not always.
If I decide to include global.cpp file inside WorldState.h, isn't it bad programming practice?
Yes.
An alternative is to define values of variable(s) dim (and marker_num) inside global.h. Is that good programming practice?
Yes, if you want to use them where compile-time constants are expected.

error: unknown type name ‘mxArray’

When I try to use MATLAB mex command to compile a c file, I met the following error
error: unknown type name ‘mxArray’
The error code is here
const char *model_to_matlab_structure(mxArray *plhs[], int num_of_feature, struct svm_model *model);
struct svm_model *matlab_matrix_to_model(const mxArray *matlab_struct, const char **error_message);
I don't understand why MATLAB doesn't recognize the mxArray type. How could I resolve this? Thanks!
Thanks. It turns out that I forgot
#include "mex.h"

Performing system calls from MATLAB Mex files

In MATLAB itself, it is very easy to call system commands, such as the following:
>> system('ls');
yprime.c yprime.mexa64
(Note: using Ubuntu)
From C(++) programs, I can execute system commands using std::system:
#include <cstdlib>
int main()
{
std::system("ls");
}
But how can I execute system commands from MATLAB Mex programs?
#include <cstdlib>
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
std::system("ls");
}
^^The above compiles, but doesn't output anything to screen when run.
I think you should have a look at that thread:
Capturing stdout from a system() command optimally
#include <stdio.h>
FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);