6

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
4

3 に答える 3

5

struct('field', {a b c})は、セル配列と同じサイズの構造体配列を作成し、セルの各要素を構造体の対応する要素のフィールド「フィールド」に配置する構造体コンストラクターの特殊な形式です。つまり、これ:

s = struct('field', {a b c});

これと同じ結果が生成されます。

s(1).field = a;
s(2).field = b;
s(3).field = c;

問題の解決策は、角かっこを使用して、次のような通常の(セル以外の)配列を形成することです。

matrixStruct = struct('matrix', [4, 4, 4; 5, 5, 5; 6, 6 ,6]);
于 2010-05-12T00:58:24.897 に答える
1

MATLABのセル配列である変数にアクセスしようとしています。結果としてデータが保存されますか?構造体にdouble配列を入れるとどうなりますか?

matrixStruct = struct('matrix', [4, 4, 4; 5, 5, 5; 6, 6 ,6])

問題は、MATLABがデータをセル配列に格納する方法にあると思います。これを実行してみてください:

double1 = 1;
double2 = 1:2;
cellempty = {[]};
celldouble1 = {1};
celldouble2 = {1:2};
cell2doubles = {1,2};
whos

出力:

Name              Size            Bytes  Class     Attributes
  cell2doubles      1x2               136  cell                
  celldouble1       1x1                68  cell                
  celldouble2       1x1                76  cell                
  cellempty         1x1                60  cell                
  double1           1x1                 8  double              
  double2           1x2                16  double              

セル配列の各要素が、数値のサイズに加えて60バイトを占めることがわかります。

于 2010-05-11T23:54:31.027 に答える
0

私はこれを経験しました:私は行列であるフィールドを持つ構造体を持っています。C ++では、対応する構造はdouble**たとえばです。でフィールドにアクセスしようとするとengGetVariable(engine,MyStruct.theField)失敗します。一時変数を使用してを格納してMyStruct.theFieldからを使用し、構造体から行列フィールドを取得するengGetVariable(engine, tempVar)コードは次のようになります。

// Fetch struct field using a temp variable
std::string tempName = std::string(field_name) + "_temp";
std::string fetchField = tempName + " = " + std::string(struct_name) 
        + "." + std::string(field_name) + "; ";
matlabExecute(ep, fetchField);
mxArray *matlabArray = engGetVariable(ep, tempName.c_str());

// Get variable elements
const int count = mxGetNumberOfElements(matlabArray);
T *data = (T*) mxGetData(matlabArray);
for (int i = 0; i < count; i++)
    vector[i] = _isnan(data[i]) ? (T) (int) -9999 : (T) data[i];

// Clear temp variable
std::string clearTempVar = "clear " + tempName + "; ";
matlabExecute(ep, clearTempVar);

// Destroy mx object
mxDestroyArray(matlabArray);

マトリックスフィールドを構造体に設定するのと非常によく似ています。

// Create temp variable
mxArray* array = convertVectorToMxArray(mat, nb_rows, nb_cols);  
const std::string temp_name = array_name + "_temp";
int ret = engPutVariable(ep, temp_name.c_str(), array);

// Set variable to struct field
const std::string cmd = std::string(array_name + " = " + temp_name + "; ");
matlabExecute(ep, cmd);

// Delete array
mxDestroyArray(array);
于 2014-11-13T08:23:10.623 に答える