mxGetDimensions returning incorrect values

9 visualizaciones (últimos 30 días)
Jake el 20 de Sept. de 2012
Comentada: Artur MKRTCHYAN el 28 de Mayo de 2021
I'm having a problem getting a mex file to run properly, and I was able to isolate the culprit as mxGetDimensions. For a matrix with dimensions MxNxL (in MATLAB), the function is returning the dimensions as Mx0xN. The relevant portion of the code follows. It's part of a code that will be run on the GPU but the problem occurs before I even do anything with the GPU.
#include "cuda.h"
#include "mex.h"
#include "math.h"
/*Main mex function*/
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
int L,N,M,dims,gridDim_y,i;
const int *dim_array;
double *vUoldr, *vUoldi, *vUnewr, *vUnewi;
float *fvUoldr, *fvUoldi;
/*GPU variables:*/
float *Uoldr_gpu, *Uoldi_gpu;
/*Find the dimensions of the input cube*/
dims = (int)mxGetNumberOfDimensions(prhs[0]);
dim_array = (const int*)mxGetDimensions(prhs[0]);
M = dim_array[0];
N = dim_array[1];
L = dim_array[2];
printf("%d %d %d %d\n",dims,M, N, L);
In case something in my compilation is the issue here are the commands I'm using to compile:
!/usr/local/cuda/bin/nvcc -c -m64 -arch=sm_30 -Xcompiler -fPIC -I /Applications/
mex -largeArrayDims test.o
and my MATLAB version is 2011a on Mac OS 10.7.4. Any ideas would be helpful as I'm sort of stumped at this point.
  1 comentario
Jan el 21 de Sept. de 2012
Editada: Jan el 21 de Sept. de 2012
@Jake: You use a meaningful title, useful tags (I add some others), you have spent time to isolate the problem and describe it clearly. The question contains the required code, though it is not formatted perfectly (I mark the code and hit the "{} Code" button for the last piece of perfection). You show the compile command, which can be useful for Mex problems and finally you mention the Matlab an OS version. Thanks for asking a good question! +1

Iniciar sesión para comentar.

Respuesta aceptada

Jan el 21 de Sept. de 2012
Editada: Jan el 22 de Sept. de 2012
This fails on 64 bit machines, which use 32 bit for type INT:
dims = (int)mxGetNumberOfDimensions(prhs[0]);
dim_array = (const int*) mxGetDimensions(prhs[0]);
Better and working under 32 and 64 bit:
const mwSize *dim_array, dims;
dims = mxGetNumberOfDimensions(prhs[0]);
dim_array = mxGetDimensions(prhs[0]); // No casting needed, thanks James
  6 comentarios
Bayes el 20 de Jun. de 2018
Thank you very much. It works well. I should have paid more attention on it.
Artur MKRTCHYAN el 28 de Mayo de 2021
Thanks a lot !!

Iniciar sesión para comentar.

Más respuestas (1)

Malcolm Lidierth
Malcolm Lidierth el 21 de Sept. de 2012
Is N correct? I am rusty with mex but with -largeArrayDims, mxGetDimensions returns size_t results. See MATLAB docs for mwSize


Más información sobre Search Path en Help Center y File Exchange.



Community Treasure Hunt

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

Start Hunting!

Translated by