问题
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!
回答1:
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)
来源:https://stackoverflow.com/questions/31642102/read-4d-array-in-mex-file