Hello, I was wondering if there was a way to change a array full of float values to an array of double values and then reference the new array to the value of a pointer?
Thank You
Hello, I was wondering if there was a way to change a array full of float values to an array of double values and then reference the new array to the value of a pointer?
Thank You
I used a FOR loop to cast each variable in the array up to a data type double. But when I try and set the array to the pointer like:
*vrp = hab2;
where hab2 is a array of 300 units, it tells me that they are incompatible data types?
Thank You
No. Since every array element uses 4 byes for float vs. 8 bytes for double, you can not cast the entire array at once.
Do you want a copy of the values in a separate array, or do you want both arrays to point to the same place? If the later, there is no point, since a double and a float are stored differently.
You should post that code that you tried.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Here is the code that I am having problems with. Notice
I'm integrating MATLAB, C and FORTRAN together through a Mex function, and thus using API's to get information to and from FORTRAN and MATLAB.Code:#include "config.h" #include "mex.h" #include "f77-fcn.h" extern void F77_FUNC (contrl, CONTRL) (float *phi, float *delta); void mexFunction (int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) { mwIndex i; mwSize n; double *vri, *vro; double hab1, hab2[2]; float heg1, heg2[2]; float guo[300]; int k; if (nrhs != 1 || ! mxIsNumeric (prhs[0])) mexErrMsgTxt ("expects matrix"); n = mxGetNumberOfElements (prhs[0]); plhs[0] = (mxArray *) mxCreateNumericArray (mxGetNumberOfDimensions (prhs[0]), mxGetDimensions (prhs[0]), mxGetClassID (prhs[0]), mxIsComplex (prhs[0])); vri = mxGetPr (prhs[0]); vro = mxGetPr (plhs[0]); if (mxIsComplex (prhs[0])) { double *vii, *vio; vii = mxGetPi (prhs[0]); vio = mxGetPi (plhs[0]); for (i = 0; i < n; i++) { vro [i] = vri [i] * vri [i] - vii [i] * vii [i]; vio [i] = 2 * vri [i] * vii [i]; } } else { hab1 = *vri; heg1 = (float)hab1; F77_FUNC (contrl, CONTRL) (&heg1,(float *)guo); heg2 [1] = guo[13]; heg2 [2] = guo[20]; //***** Important Part********* for (k = 0; k < 3; k++) vro[k] = (double)heg2[k]; //This Line is Having PROBLEMS //I create the mex file using Octave and this part does not work } }
C still has to abide by all the normal rules inside this wrapper. But using the code above or:
still leaves me with problems.Code://***** Important Part********* for (k = 0; k < 3; k++) hab2[k] = (double)heg2[k]; *vro = hab2; //This Line is Having PROBLEMS
*****I am able to cast the array properly, but I am having problems assigning the array to the value of the pointer*****
Thank you for your help
What kinds of problems are you having? Specifically.
if I use :
Then MATLAB or Octave both Crash while making the mex fileCode://***** Important Part********* for (k = 0; k < 3; k++) vro[k] = (double)heg2[k]; //This Line is Having PROBLEMS //I create the mex file using Octave and this part does not work
If I use :
The programs tell me: error: incompatible types in assignmentCode://***** Important Part********* for (k = 0; k < 3; k++) hab2[k] = (double)heg2[k]; *vro = hab2; //This Line is Having PROBLEMS
The wrapper can send out a single value very well, but I need a array to come out of the wrapper.
Thank You
So having looked up all these mxfunctions, if you want an array of doubles, shouldn't you create an array of doubles with your plhs[0] thing? It looks like you are not.
*vro = hab2; //This Line is Having PROBLEMS
That line should be:
* dereferences the pointer.Code:vro = hab2;
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Awesome! Thanks Guys. No crashing anymore and the function is now outputting a array of zeros. I'm happier :-). I think there may be another problem with how I am declaring the mxCreateDoubleMatrix, but thats not a C issue.
Thank You so Much for all your help! Hope everyone have a nice weekend