Clear Filters
Clear Filters

MATLAB Compiler API for .NETでデータの受​け渡しを行う場合、多​次元配列フィールド値​をコマンドライン上で​確認する方法

4 views (last 30 days)
啓嗣
啓嗣 on 16 Jan 2024
Commented: 啓嗣 on 16 Jan 2024
お疲れ様です。
MATLAB Compiler SDKを用いて、MATLAB関数をパッケージ化し、.NETアセンブリに統合したいと考えています。
構造体のデータ型でデータをやり取りしようとした際、MATLABから出力された構造体の内、一つのフィールド値について、GetFieldを用いて値を取得しようとすると以下のエラーが出現します。
エラー内容:
System.InvalidOperationException
HResult=0x80131509
Message=Accessing properties and methods not supported for MATLAB struct.
Source=MathWorks.MATLAB.Types
スタック トレース:
場所 MathWorks.MATLAB.Types.MATLABArray.TryInvokeMember(InvokeMemberBinder binder, Object[] args,   
    Object& result)
場所 System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
場所 namespace名.Program.Main(String[] args) (C:\Users\...\Program.cs):行 105
.NET側で構造体を定義し、MATLAB側で構造体のフィールド値に関する値を計算します。
その後、MATLAB側から更新された構造体を.NET側に返します。この際、.NET側は更新された構造体のフィールドの値を確認したいと考えています。
Program.cs:
class Program
{
private static Double[,] X = new Double[,] {};
private static Double[,] Y = new Double[,] {};
private static MATLABStruct mystr = new MATLABStruct(("X",X),("Y",Y));
static void Main(string[] args)
{
// % 出力定義
dynamic mystr_out
MATLABFunctions.example(matlab, mystr, out mystr_out)
Double[,] field_fp = mystr_out.GetField("fp"); // % 例外発生個所
mystr = mystr_out;
}
}
問題を解決するため、お力添えいただければ幸いです。
  5 Comments
Kojiro Saito
Kojiro Saito on 16 Jan 2024
出力のデータ型をMATLABArrayからMATLABStructに変更したら例外処理が起こらなくなったということは、 配列に対して構造体のメソッド(GetField)を行おうとしていたのかなと考えられます。
啓嗣
啓嗣 on 16 Jan 2024
テストで作成したコードは出力型がMATLABArrayでもGetFieldを行えているのですが、両者の違いとしては何が考えられるのでしょうか。
Program.cs
using System;
using MathWorks.MATLAB.Runtime;
using MathWorks.MATLAB.Types;
using testStruct;
using System.Collections.Generic;
namespace testStruct_App
{
class Program
{
public static int ind =1;
public static Double[,] fp = new double[,] { };
public static Double[,] mv = new double[,] { };
public static MATLABStruct in1 = new MATLABStruct(("fp", fp));
static void Main(string[] args)
{
string ctfPath = @"testStruct.ctf";
using (dynamic matlab = MATLABRuntime.StartMATLAB(ctfPath))
{
while (ind <= 3)
{
if(ind == 1)
{
Double[,] coll_pt = new Double[,] {{ 0.9 },{ 0.7 },{ 1 }};
// % 出力の定義
dynamic out1;
MATLABFunctions.testStruct(matlab, coll_pt, mv, in1, out out1);
// % 構造体のフィールド(field1)を取得
Double[,] fp_val = out1.GetField("fp");
Double[,] mv_val = out1.GetField("mv");
Disp_array(fp_val);
Disp_array(mv_val);
}
ind++;
} // while
} // ctf
} // Main
static void Disp_array<T>(T[,] myarray)
{
for (int i = 0; i < myarray.GetLength(0); i++)
{
for (int j = 0; j < myarray.GetLength(1); j++)
{
Console.Write("{0} ", myarray[i, j]);
}
Console.WriteLine();
}
}
}
}
testStruct.cs
/* File: testStruct.cs
*
* MATLAB Strongly Typed Interface Version: R2023b
* C# source code generated on: 2024/01/16
*/
using System;
using MathWorks.MATLAB.Types;
using MathWorks.MATLAB.Exceptions;
namespace testStruct{
public static partial class MATLABFunctions {
public static void testStruct(MATLABProvider _matlab, dynamic coll_pt, dynamic mv, dynamic str){
dynamic _dynMatlab = _matlab;
_dynMatlab.testStruct(new RunOptions(nargout:0),coll_pt,mv,str);
}
public static void testStruct(MATLABProvider _matlab, dynamic coll_pt, dynamic mv, dynamic str, out dynamic out1){
dynamic _dynMatlab = _matlab;
out1 = (MATLABArray)_dynMatlab.testStruct(new RunOptions(nargout:1),coll_pt,mv,str);
}
}
}
testStruct.m
function [out1]= testStruct(coll_pt,mv,str)
arguments(Input)
coll_pt double
mv double
str struct
end
arguments(Output)
out1 struct
end
str=collision_struct(coll_pt,str);
str.mv = mv;
out1 = str;
end
collision_struct.m
function [str_out]= collision_struct(coll_pt,str)
str.fp = coll_pt;
str_out = str;
end

Sign in to comment.

Answers (0)

Products


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!