C++ pointers and variables

Dear experts,
I am trying to convert the following C++ code to a matlab code:
double buffer = 72.0;
double *pBuffer = &buffer;
t = sizeof(buffer));
Does anyone know to solve this question?
Thank you in advance.
Carlos

 Accepted Answer

Jan
Jan on 3 Apr 2017
buffer = 72;
You do neither need a pointer in Matlab nor does the the size of the type matter.

4 Comments

Hello Jan! Thank you for your quick reply. Certainly, my problem is a bit complex. I have to convert C code y matlab code because I want to include this code in a GUI. On the one hand, I have the C functions within a file .h. Concretely, I would like to convert this C code lines in matlab commands:
double buffer = 72.0;
double *pBuffer = &buffer;
SetSystemParameter(MAXIMUM_RANGE,pBuffer,sizeof(buffer));
where SetSystemParameter is a function implemented in the library h and MAXIMUM_RANGE is a constant. The function SetSystemParameter sets the value provided in buffer but an error is generated if the buffer is incorrectly sized.
On the other hand, I use the following commands in matlab in order to obtain the same results as in C:
libstring = 'ATC3DG64'; %library h
buffer = double(72);
pRecord31 = libpointer('doublePtr',buffer);
calllib(libstring,'SetSystemParameter',MAXIMUM_RANGE,pRecord31,sizeof(buffer));
Sizeof is a .m file downloaded from matlab central. The author says this function does the same as the sizeof function of C code. The problem is my program alway returns an error related to the buffer size and I don't know why since it perfectly works in C.
Can you help me?
Thank you Jan!
The esiest method would be to let the C-part in C and include it in a C-Mex function. This is a C-function with the entry point:
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Here you can get the contents of the Matlab variables as C-arrays and after the calculations create Matlab variables again for replying. The C-Mex files are compiled by the mex command and can be used like normal M-functions.
Thank you Jan. I made it.
If you're using this filexchange sizeof you're using it incorrectly. The correct code should have been:
calllib(libstring,'SetSystemParameter',MAXIMUM_RANGE,pRecord31,sizeof('double'));
Or you could just not have bothered and pass 8 directly, since scalar double are always 8 bytes.
Note that matlab may have been clever enough to automatically make the conversion from double to void pointer, so possibly this may have been enough:
value = 72;
calllib(libstring,'SetSystemParameter',MAXIMUM_RANGE, value, 8)

Sign in to comment.

More Answers (0)

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Tags

Asked:

on 3 Apr 2017

Commented:

on 4 Apr 2017

Community Treasure Hunt

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

Start Hunting!