在MEX文件中读取4D阵列

时间:2015-07-26 21:23:17

标签: arrays matlab mex

我在MATLAB中有一个4维数组;我试图在MEX函数中访问该数组。下面创建'testmatrix',这是一个包含uint8类型已知数据的4维矩阵。

%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);

但是,当我尝试使用mxGetNumberOfDimensions时,我的尺码不正确,我的代码如下:

#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);

}

testarray上的这个mex文件的结果如下:

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

dims对于第一个元素是正确的,但其他一切都是错误的 - 我必须做一些根本不正确的事情。任何想法或正确方向的指针都将受到赞赏;谢谢大家!

1 个答案:

答案 0 :(得分:3)

当您调用类似

的功能时
rgb_to_uint8 testmatrix

相当于像这样调用它

rgb_to_uint8('testmatrix')

与此相同:

c = 'testmatrix'
rgb_to_uint8(c)

当然是大小为1x10(2D)的字符数组。你需要像函数一样调用:

rgb_to_uint8(testmatrix)