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

 Respuesta aceptada

Jan
Jan el 3 de Abr. de 2017

0 votos

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

4 comentarios

Carlos
Carlos el 3 de Abr. de 2017
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!
Jan
Jan el 3 de Abr. de 2017
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.
Carlos
Carlos el 4 de Abr. de 2017
Thank you Jan. I made it.
Guillaume
Guillaume el 4 de Abr. de 2017
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)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Operators and Elementary Operations en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 3 de Abr. de 2017

Comentada:

el 4 de Abr. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by