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

 Akzeptierte Antwort

Jan
Jan am 3 Apr. 2017

0 Stimmen

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

4 Kommentare

Carlos
Carlos am 3 Apr. 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 am 3 Apr. 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 am 4 Apr. 2017
Thank you Jan. I made it.
Guillaume
Guillaume am 4 Apr. 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)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Operators and Elementary Operations finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 3 Apr. 2017

Kommentiert:

am 4 Apr. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by