20288

Read 4D Array in MEX File

I have a 4 dimensional array in MATLAB; I am trying to access the array in a MEX function. The following creates 'testmatrix', a matrix of 4 dimensions with known data of uint8 type.

%Create a 4D array 2x,2y, rgb(3), framenumber(from 1 to 5) %Framenumber from 1 to 5 testmatrix(1,1,1,1) = 0; testmatrix(1,1,1,2) = 1; testmatrix(1,1,1,3) = 2; testmatrix(1,1,1,4) = 3; testmatrix(1,1,1,5) = 4; testmatrix(1,1,2,1) = 5; testmatrix(1,1,2,2) = 6; testmatrix(1,1,2,3) = 7; testmatrix(1,1,2,4) = 8; testmatrix(1,1,2,5) = 9; testmatrix(1,1,3,1) = 10; testmatrix(1,1,3,2) = 11; testmatrix(1,1,3,3) = 12; testmatrix(1,1,3,4) = 13; testmatrix(1,1,3,5) = 14; testmatrix(1,2,1,1) = 15; testmatrix(1,2,1,2) = 16; testmatrix(1,2,1,3) = 17; testmatrix(1,2,1,4) = 18; testmatrix(1,2,1,5) = 19; testmatrix(1,2,2,1) = 20; testmatrix(1,2,2,2) = 21; testmatrix(1,2,2,3) = 22; testmatrix(1,2,2,4) = 23; testmatrix(1,2,2,5) = 24; testmatrix(1,2,3,1) = 25; testmatrix(1,2,3,2) = 26; testmatrix(1,2,3,3) = 27; testmatrix(1,2,3,4) = 28; testmatrix(1,2,3,5) = 29; testmatrix(2,1,1,1) = 30; testmatrix(2,1,1,2) = 31; testmatrix(2,1,1,3) = 32; testmatrix(2,1,1,4) = 33; testmatrix(2,1,1,5) = 34; testmatrix(2,1,2,1) = 35; testmatrix(2,1,2,2) = 36; testmatrix(2,1,2,3) = 37; testmatrix(2,1,2,4) = 38; testmatrix(2,1,2,5) = 39; testmatrix(2,1,3,1) = 40; testmatrix(2,1,3,2) = 41; testmatrix(2,1,3,3) = 42; testmatrix(2,1,3,4) = 43; testmatrix(2,1,3,5) = 44; testmatrix(2,2,1,1) = 45; testmatrix(2,2,1,2) = 46; testmatrix(2,2,1,3) = 47; testmatrix(2,2,1,4) = 48; testmatrix(2,2,1,5) = 49; testmatrix(2,2,2,1) = 50; testmatrix(2,2,2,2) = 51; testmatrix(2,2,2,3) = 52; testmatrix(2,2,2,4) = 53; testmatrix(2,2,2,5) = 54; testmatrix(2,2,3,1) = 55; testmatrix(2,2,3,2) = 56; testmatrix(2,2,3,3) = 57; testmatrix(2,2,3,4) = 58; testmatrix(2,2,3,5) = 59; testmatrix = uint8(testmatrix);

However, I am getting incorrect dimensions when I try to use mxGetNumberOfDimensions my code is as follows:

#include "mex.h" void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[]) { //Check - expect 1 input, expect 1 outputs if (nrhs != 1) { mexErrMsgIdAndTxt( "MATLAB:24bit_pixel_from_uint8_rgb:invalidNumInputs", "One input argument required."); } if (nlhs > 1) { mexErrMsgIdAndTxt( "MATLAB:24bit_pixel_from_uint8_rgb:maxlhs", "Too many output arguments."); } //declare variables mxArray *a_in_m, *b_out_m; double *a, *b, *c, *d; int dimx, dimy, numdims; int dima, dimb, dimc, dimd; int i,j,M,N,L,W; //Comment mwSize *dim_array, dims; dims = mxGetNumberOfDimensions(prhs[0]); dim_array = mxGetDimensions(prhs[0]); M = dim_array[0]; N = dim_array[1]; L = dim_array[2]; W = dim_array[3]; mexPrintf("dims: %d\n",dims); mexPrintf("M: %d\n",M); mexPrintf("N: %d\n",N); mexPrintf("L: %d\n",L); mexPrintf("W: %d\n",W); }

The result of this mex file on testarray follows:

>> rgb_to_uint8 testmatrix dims: 2 M: 1 N: 10 L: 1634887535 W: 1766203501

dims is correct for the first element, but everything else is wrong - I must be doing something fundamentally incorrect. Any thoughts or a pointer in the right direction would be appreciated; thanks all!

Answer1:

When you call a function like

rgb_to_uint8 testmatrix

that is equivalent to calling it like this

rgb_to_uint8('testmatrix')

which is the same as this:

c = 'testmatrix' rgb_to_uint8(c)

which of course is a character array of size 1x10 (2D). You need to call like a function:

rgb_to_uint8(testmatrix)

Recommend

  • Some help writing a s-function to read data from serial port
  • using integer arrays on mex
  • Evaluate string to class variable
  • Error using DGESV in Matlab mex
  • How to add header files path relative to the current file?
  • Matlab Codegen Eig Function - Is this a Bug?
  • How to check if variable is Scalar or Array before mxIsScalar was introduced?
  • Using Matlab “engine.h” from c++ correctly
  • drawing random circles, storing their coorindates in an array
  • How to pass EC named curve list used by SSL_CTX?
  • How to read piped content in C?
  • Regarding starting the threads on a condition
  • Simple linked list-C
  • SQLite connection strategies
  • How to calculate the camera position from Vuforia GL matrix?
  • Checking if an array in C is symmetric
  • Reduction and collapse clauses in OMP have some confusing points
  • Appending Character to Character Array In C
  • C: Incompatible pointer type initializing
  • OOP Javascript - Is “get property” method necessary?
  • GridView breaks while scrolling
  • copying resource to sdcard gives a damaged file in android
  • Database structure design with variable amounts of fields
  • How to avoid particles glitching together in an elastic particle collision simulator?
  • how to adjust image in a panel in Java swing?
  • Recording logins for password protected directories
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Splitting given String into two variables - php
  • Problems to linebreak with an int in JLabel
  • How reduce the height of an mschart by breaking up the y-axis
  • AES padding and writing the ciphertext to a disk file
  • VS2008 Enable C++ Exception with SEH
  • output of program is not same as passed argument
  • Check if a string to interpolate provides expected placeholders
  • MySQL WHERE-condition in procedure ignored
  • Rearranging Cells in UITableView Bug & Saving Changes
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Sorting a 2D array using the second column C++
  • java string with new operator and a literal