Deploy an array with type MATLAB Class to .NET through Data API

1 次查看(过去 30 天)
I have a class with properties that look something like this:
classdef MyMatlabClass
properties
name (1,1) string
innerClass (1,:) classes.MyInnerClass
end
end
I want my innerClass to look like an array in .NET (i.e. be a MyInnerClass[] when I query it from MyMatlabClass), but I'm just getting a type of MyInnerClass (not enumerable). When using a double as the property rather than a class, I am getting a double[] and I'm able to index and access it as expected. I've been following the documentation found here ( https://www.mathworks.com/help/compiler_sdk/dotnet/deploy-matlab-classes-to-dotnetapp-using-mda.html ) to do this conversion as well as the strongly typed docs here ( https://www.mathworks.com/help/matlab/matlab_external/data-type-mappings-between-net-and-strongly-typed-matlab-code.html ). I see the limitations in the latter documentation and this use case doesn't seem to be listed there so I hope I'm just missing something.
Thank you for any help,
Matthew

采纳的回答

SOUMNATH PAUL
SOUMNATH PAUL 2023-11-23
编辑:SOUMNATH PAUL 2023-11-23
To my understanding you're trying to use MATLAB classes in a .NET environment, and you want the innerClass" property to be treated as an array in .NET.
MATLAB does not directly support converting custom classes to .NET arrays, especially when the class contains other custom classes.
Here are some suggestions to mitigate this issue:
  1. Try creating a method in your MATLAB class that returns an array of "MyInnerClass" objects, and then calling that method from your .NET code to retrieve the array. This way, you can have more control over how the data is converted between MATLAB and .NET.
classdef MyMatlabClass
properties
name (1,1) string
innerClass (1,:) classes.MyInnerClass
end
methods
function innerArray = getInnerClassArray(obj)
innerArray = obj.innerClass;
end
end
end
In your .NET code, you can call the “getInnerClassArray” method to obtain the array
MyMatlabClass myObject = new MyMatlabClass();
MyInnerClass[] innerArray = myObject.getInnerClassArray();
2. You can also consider implementing custom serialization methods in your MATLAB class to convert it into a format that can be easily deserialized in .NET. You would need to write methods to serialize your MATLAB class into a standard format (e.g., JSON or XML), and then deserialize it in your .NET code.
Hope it helps!
Regards,
Soumnath
  1 个评论
Matthew Trahan
Matthew Trahan 2023-12-5
Thanks, Soumnath. This didn't quite work, but you did help me get on the right track. When using this approach I got the following error:
Accessing properties and methods not supported for nonscalar MATLAB objects.
Your answer, though, helped me realize that I can pass in an index to a Matlab function and return the class object at that location in the Matlab array.
With that being said, your idea in point 2 is probably much cleaner than dealing with all of this.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Call Web Services from MATLAB Using HTTP 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by