How do I pass variables from a MATLAB script to C++?

I would like to pass data stored in a MATLAB script variable to a C++ variable.
Please could someone let me know how I can implement this. Some step-by-step instructions would be very useful.
Many thanks.

2 Comments

Paul - what do you intend to do with the variable passed to the C++ function? Return something back to MATLAB? See write C/C++ source files for some examples on writing C/C++ functions and classes, and how to pass data from MATLAB to the function.
Geoff - thanks for your reply and the link. I indend to pass the variable one way. I am using OpenTLD and would like to use the tracked object's current XY co-ordinates to control the mouse cursor via the C++ function, SetCursorPos(). Therefore, the co-ordinates would need to passed from the TLD MATLAB script to my C++ code everytime they change. What is the best/easist method of doing this? I am quite new to MATLAB. Thank you.

Sign in to comment.

Answers (2)

Hi,
Why don't you use JAVA for this? It is way easier to move the mouse with JAVA than with platform dependent c++ code. See the Robot Class:
So the MATLAB code would be:
robot = java.awt.Robot()
robot.mouseMove(0,0)
I can't be any easier than this I think.
Paul - if you just want to call the Windows SetCursorPos API from within MATLAB, then you can create a simple C-file based on the link I posted in my comment
#include "mex.h"
#include <windows.h>
#include <winuser.h>
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] )
{
double *ptr = 0;
double xOffset = 0;
double yOffset = 0;
HWND window = GetDesktopWindow;
// get the input offsets
if (nrhs>=1)
{
ptr = mxGetPr(prhs[0]);
xOffset = *ptr;
}
if (nrhs>=2)
{
ptr = mxGetPr(prhs[1]);
yOffset = *ptr;
}
printf("offsets are %f %f\n", xOffset, yOffset);
if (window)
{
RECT rect = {0};
GetClientRect(window, &rect);
SetCursorPos(rect.right+xOffset, rect.bottom+yOffset);
}
}
Just save this file as (for example) setcursropos.c (which will be the name of the MEX function that you will call from your MATLAB code) and build the MEX file by typing the following in the (MATLAB) Command Window
mex setcursropos.c
If MEX has been set up with a C/C++ compiler, you should see something like
Building with 'lcc-win32'.
MEX completed successfully.
I am using R2014a so the default compiler is lcc-win32.
The inputs to this function are offsets from the Desktop window rectangle top-left corner. You can call this function to move the cursor around from within your MATLAB code, or just from the Command Window as
% zero offsets so cursor should be set to top-left corner
setcursorpos(0,0)
% cursor should be set to (roughly) bottom-left corner
setcursorpos(0,10000)
% cursor should be set to (roughly) bottom-right corner
setcursorpos(10000,10000)
% cursor should be set to (roughly) top-right corner
setcursorpos(10000,0)
% cursor should be set to somwhere away from edges
setcursorpos(500,500)
Note how we invoke the function using the name of the C-file. You should be able to adapt the above to translate coordinates from your MATLAB code to the appropriate cursor position.
Try it and see what happens!

4 Comments

Geoff - thanks for your informative answer. I hadn't thought about doing it that way. However, when I build the MEX file, I receive 5 errors:
Error F:\Program Files\OpenTLD-SetCursorPos\setcursorpos.c: 5 redeclaration of `mexFunction' previously declared at F:\Program Files\MATLAB\R2014a\extern\include\mex.h 144
Error F:\Program Files\OpenTLD-SetCursorPos\setcursorpos.c: 14 unknown size for type `const incomplete struct mxArray_tag defined at F:\Program Files\MATLAB\R2014a\extern\include\matrix.h 246'
Error F:\Program Files\OpenTLD-SetCursorPos\setcursorpos.c: 14 type error in argument 1 to `mxGetPr'; found `incomplete struct mxArray_tag defined at F:\Program Files\MATLAB\R2014a\extern\include\matrix.h 246' expected`pointer to const incomplete struct mxArray_tag defined at F:\Program Files\MATLAB\R2014a\extern\include\matrix.h 246'
Error F:\Program Files\OpenTLD-SetCursorPos\setcursorpos.c: 19 unknown size for type `const incomplete struct mxArray_tag defined at F:\Program Files\MATLAB\R2014a\extern\include\matrix.h 246'
Error F:\Program Files\OpenTLD-SetCursorPos\setcursorpos.c: 19 type error in argument 1 to `mxGetPr'; found `incomplete struct mxArray_tag defined at F:\Program Files\MATLAB\R2014a\extern\include\matrix.h 246' expected `pointer to const incomplete struct mxArray_tag defined at F:\Program Files\MATLAB\R2014a\extern\include\matrix.h 246'
Please could you assist me with this.
Thank you.
Paul - have you created the setcursorpos.c exactly as above or have you modified it? I just copied the above into another file and was able to build it again (and use it). Could you attach your file using the paperclip button so that I can try and build it?
Also, what is the line of code that you are running from the MATLAB Command Window to build it? Is it just mex setcursorpos.c.
Geoff - I just copied your code without modifing it. I was not sure how to create a new C file in MATLAB, so I create a txt file and changed the extension to .c. Maybe this is why it is not building? I've attached the file as a zip (.c files can't be attached).
I am running mex setcursorpos.c.
Thank you.
Paul - I just compiled your file thinking that I wouldn't see the same error and I did! There is just one bug in the code. The mexFunction signature is (in your file) is defined as
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray prhs[] )
which is slightly different than mine. Check the last input parameter - prhs should be a pointer like plhs. So because this signature is different from the standard mexFunction signature, then the redeclaration error is clear.
Just change the signature to
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
recompile, and it should work. (It did for me!)

Sign in to comment.

Categories

Products

Tags

Asked:

on 23 Aug 2014

Edited:

on 25 Aug 2014

Community Treasure Hunt

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

Start Hunting!