1

これは、スカラーではなく配列を返すことができるようにするためのこの質問の拡張です。

matlab コーダーを介して matlab コードから生成された C コードは、問題ないように見えます (以下を参照)。結果を C# の世界に戻す方法を見つけようとしています。これが私の最初の試みです:

C# コード

[DllImport(@"C:\bla\CPlusPlus.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void test(ref emxArray_real_T a, ref emxArray_real_T result);

static void Main(string[] args)
{
    double[,] array2D = new double[,] { { 1, 2, 4 }, { 1, 3, 4 } };
    var wrapper = new EmxArrayRealTWrapper(array2D);

    var t = wrapper.Value;
    var t1 = wrapper.Value;
    test(ref t, ref t1);
}

public class EmxArrayRealTWrapper : IDisposable
{
private readonly emxArray_real_T _value;
private GCHandle _dataHandle;
private GCHandle _sizeHandle;

public emxArray_real_T Value
{
    get { return _value; }
}

public EmxArrayRealTWrapper(double[,] data)
{
    _dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
    _value.data = _dataHandle.AddrOfPinnedObject();
    _sizeHandle = GCHandle.Alloc(new int[] { data.GetLength(0), data.GetLength(1) }, GCHandleType.Pinned);
    _value.size = _sizeHandle.AddrOfPinnedObject();
    _value.allocatedSize = data.GetLength(0) * data.GetLength(1);
    _value.numDimensions = 2;
    _value.canFreeData = false;
}

public void Dispose()
{
    _dataHandle.Free();
    _sizeHandle.Free();
    GC.SuppressFinalize(this);
}

~EmxArrayRealTWrapper()
{
    Dispose();
}
}

[StructLayout(LayoutKind.Sequential)]
public struct emxArray_real_T
{
public IntPtr data;
public IntPtr size;
public int allocatedSize;
public int numDimensions;
[MarshalAs(UnmanagedType.U1)]
public bool canFreeData;
}

Matlab コード:

function [result] = test(a, result)
%#codegen

if(~isempty(coder.target))
    assert(isa(a,'double'));
    assert(all(size(a) == [1 Inf]));
    assert(isa(result,'double'));
    assert(all(size(result) == [1 Inf]));
end

result = sum(a);

生成された C コード

void test(const emxArray_real_T *a, emxArray_real_T *result)
{
  real_T y;
  int32_T k;
  if (a->size[1] == 0) {
    y = 0.0;
  } else {
    y = a->data[0];
    for (k = 2; k <= a->size[1]; k++) {
      y += a->data[k - 1];
    }
  }

  k = result->size[0] * result->size[1];
  result->size[0] = 1;
  result->size[1] = 1;
  emxEnsureCapacity((emxArray__common *)result, k, (int32_T)sizeof(real_T));
  result->data[0] = y;
}

PS:

デビッドの答えを考えると、私は現時点で次のようなことを試みています:

[DllImport(@"C:\bla\CPlusPlus.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void test(ref emxArray_real_T a, ref emxArray_real_T result);

static void Main(string[] args)
{
    double[,] array2D = new double[,] { { 1, 2, 4 }, { 1, 3, 4 } };
    double[,] temp = new double[,] { { 0 }, { 0 } };
    var wrapper = new EmxArrayRealTWrapper(array2D);
    var wrapper1 = new EmxArrayRealTWrapper(temp);

    var t = wrapper.Value;
    var t1 = wrapper1.Value;
    test(ref t, ref t1);

    // initialise this by your call to the native code
    int[] size = new int[2];
    Marshal.Copy(t1.size, size, 0, 2);
    int nCol = size[0];
    int nRow = size[1];
    double[] data = new double[nCol * nRow];
    Marshal.Copy(t1.data, data, 0, nCol * nRow);
}

これにより、エントリが 1 つだけ得られます。7 つの nCol と nRow は 1 に等しくなります。

4

1 に答える 1

1

emxArray_real_T基本的に、 a の内容をC# オブジェクトに読み込む方法を尋ねています。

最初に 1D 配列を考えてみましょう。次のように読みます。

emxArray_real_T result;
// initialise this by your call to the native code
int size = Marshal.ReadInt32(result.size);
double[] data = new double[size];
Marshal.Copy(result.data, data, 0, size);

以上です。あなたはそれを主張したいでしょうresult.numDimensions == 1

また、この手順を実行する必要がない場合もありますMarshal.Copy。おそらく、渡した配列にまだアクセスresult.dataできるので、そのまま使用できます。

2 次元の場合は、ほぼ同じです。もう一度確認してくださいresult.numDimensions == 2

int[] size = new int[2];
Marshal.Copy(result.size, size, 0, 2);
int nCol = size[0];
int nRow = size[1];
double[] data = new double[nCol * nRow];
Marshal.Copy(result.data, data, 0, nCol * nRow);

これにより、データが 1 次元配列に配置されます。おそらく、これを 2 次元のマネージ配列に配置する必要があります。MATLAB が列優先であると仮定すると、列優先から行優先への変換に対処する必要があります。

double[,] arr = new double[nRow, nCol];
int index = 0;
for (int col = 0; col<nCol; col++)
{
    for (int row = 0; row<nRow; row++)
    {
        array[row, col] = data[index];
        index++;
    }
}
于 2013-02-23T17:29:54.567 に答える