mex関数からmatlab構造体のフィールドに格納されている行列にアクセスする方法を理解しようとしています。
それはひどく長い風です...私に説明させてください:
次のように定義されたmatlab構造体があります。
matrixStruct = struct('matrix', {4, 4, 4; 5, 5, 5; 6, 6 ,6})
行列の最初の要素(c用語ではmatrix [0] [0])へのポインターを受け取りたいmex関数がありますが、その方法がわかりません。それ。
私は以下を試しました:
/* Pointer to the first element in the matrix (supposedly)... */
double *ptr = mxGetPr(mxGetField(prhs[0], 0, "matrix");
/* Incrementing the pointer to access all values in the matrix */
for(i = 0; i < 3; i++){
printf("%f\n", *(ptr + (i * 3)));
printf("%f\n", *(ptr + 1 + (i * 3)));
printf("%f\n", *(ptr + 2 + (i * 3)));
}
これが最終的に印刷されるのは次のとおりです。
4.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
私はまた、ネストされた関数呼び出しではおそらくそれが奇妙なものであると考えて、次のバリエーションを試しましたが、役に立ちませんでした:
/* Pointer to the first location of the mxArray */
mxArray *fieldValuePtr = mxGetField(prhs[0], 0, "matrix");
/* Get the double pointer to the first location in the matrix */
double *ptr = mxGetPr(fieldValuePtr);
/* Same for loop code here as written above */
私がやろうとしていること、または私が潜在的に間違っていることをどのように達成できるかについて誰かが考えを持っていますか?
ありがとう!
編集:yukのコメントによると、doubleの1次元配列であるarrayというフィールドを持つ構造体に対して同様の操作を実行してみました。
配列を含む構造体は次のように定義されます。
arrayStruct = struct('array', {4.44, 5.55, 6.66})
mex関数内からarrayStructで次のことを試しました。
mptr = mxGetPr(mxGetField(prhs[0], 0, "array"));
printf("%f\n", *(mptr));
printf("%f\n", *(mptr + 1));
printf("%f\n", *(mptr + 2));
...しかし、出力は以前に印刷されたものに従いました:
4.440000
0.000000
0.000000