How to invoke a .NET-method which expects a .NET-struct as parameter?

조회 수: 2 (최근 30일)
Martin
Martin 2025년 4월 8일
편집: Mohammad Sami 2025년 4월 23일
Hello,
is this possible at all in Matlab? I have a .NET assembly with a method that expects a .NET struct as parameter. I can't figure out how to create an appropriate struct for the method. I always get the error
"No method 'TestNETNameSpace.TestNETClass.passStructToMe' with matching signature found."
with this Matlab code or my other attempts:
asmbl = NET.addAssembly('C:\Test\testNETAssembly.dll');
import TestNETNameSpace.*
result1 = TestNETClass.passIntToMe(42); % This works fine.
% Question: How to make this right?
Matlabstruct.stringInStruct = "hello";
Matlabstruct.doubleInStruct = 12.5;
result2 = TestNETClass.passStructToMe(Matlabstruct); % This throws an error.
This is the C# -code of my assembly testNETAssembly.dll.
namespace TestNETNameSpace
{
public static class TestNETClass
{
public struct TestNETStruct
{
public string stringInStruct;
public double doubleInStruct;
}
public static string passStructToMe(TestNETStruct str)
{
return str.stringInStruct;
}
public static int passIntToMe(int i)
{
return i;
}
}
}

답변 (1개)

Mohammad Sami
Mohammad Sami 2025년 4월 23일
편집: Mohammad Sami 2025년 4월 23일
You can follow the type converstion between Matlab and .NET here
In particular your .NET function would need to accept the following type of variable.
MathWorks.MATLAB.Types.MATLABStruct
MathWorks.MATLAB.Types.MATLABStruct[]
MathWorks.MATLAB.Types.MATLABStruct[,,...]
If you are trying to use a third parties code, I assume you will then have to write your own wrapper functions in .NET which covert your matlab data into the type required for your third party library to work.

카테고리

Help CenterFile Exchange에서 Get Started with Microsoft .NET에 대해 자세히 알아보기

태그

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by