LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to access data via DotNet reference type

Solved!
Go to solution

Dear community, 

 

with Matlab Compiler SDK I create a .NET Assembly, which I include to Labview. 

 

My matlab function has signature stru = calculateFittingCoefficients(a, b, c). 

stru is a matlab struct that looks like:

maxine_mumann_0-1667902422696.png

In Labview I'd like to read out and store those key value pairs. However, I have only access to the DotNet refnum. 

maxine_mumann_1-1667902548445.png

maxine_mumann_2-1667902559695.png

For the .Net to Variant Object I have to know the datatype of the data. Matlab support did not react on my service request, so I don't know how the data is passed to the .NET object. Does anyone know how I might extract the data from Dotnet reference? Or get the data as an output?

 

Looking forward to your response.

0 Kudos
Message 1 of 11
(2,395 Views)

Hi,

use the .net property node on the 'calculatedFittingCoefficients' reference you should get your data if it's actually a struct

bharathp10_0-1667904549304.png

 

bp
0 Kudos
Message 2 of 11
(2,375 Views)

Hello bharath_p,

 

Thanks for your help. Unfortunately it gives me Property > No Properties. However if I right klick on Property Node and Create > Method for System-Object class > ToString() the output is:

1x1 struct array with fields:

e

d

The values seem to be missing. The Size of Array from calculateFittingCoefficients is one and contains the .net reference. Any other idea how to access data pr it's properties?

0 Kudos
Message 3 of 11
(2,352 Views)

You are not getting back that struct, or an array of that struct. You are getting back an array of Object.

 

You can try reading through these links to see if they are of any help.

 

https://www.mathworks.com/help/compiler_sdk/gs/create-a-dotnet-application-with-matlab-code.html

 

https://knowledge.ni.com/KnowledgeArticleDetails?id=kA03q000000YGE2CAO&l=en-US

 

If you are not able to get the variant to data node to function correctly to convert to LabVIEW data, then converting the reference to more specific class may be necessary. To do that, you'll need to determine the correct class and get a reference to it. (Or write a wrapper that returns a more specific class.)

0 Kudos
Message 4 of 11
(2,327 Views)

Hello JimB, 

 

thank you for your support. The links do not target my challenge, probably I have to go for the conversion of the reference to a more specific class approach. Could you provide further info how to do so? What do I need to look for to accomplish this?

 

My apologies for the up-to-now modest understanding of Labview usage.

Looking forward to your advice, best wishes

 

0 Kudos
Message 5 of 11
(2,256 Views)

@maxine_mumann wrote:

probably I have to go for the conversion of the reference to a more specific class approach. Could you provide further info how to do so? What do I need to look for to accomplish this?

 


Are you able to determine what the actual class or struct being returned is? I've not done this before, but step 3 of this section in the link previously shared seems to indicate that MATLAB should generate quite a bit of documentation on the assembly, including a sample C# file.

 

The lines of interest in the example shown on that page are here:

results = MagicSquareClassInstance.makesquare(1, xIn);
if (results[0] is MWNumericArray) {
    yOut = (MWNumericArray) results[0];
}

 

In this case, the actual type returned is MWNumericArray, which is a type defined in another Mathworks assembly. You would need to determine the correct return type for your function.

 

Once you know that, you will need to create a reference to an object of that type, and use the To More Specific Class function to cast the reference to that type. You should check the error wire after this as an exception may be thrown if the cast is not valid. Once the reference is cast to the correct type, you should be able to access any public properties or fields normally using the Property Node.

0 Kudos
Message 6 of 11
(2,228 Views)

Hi,

As mentioned in previous post check your assembly and function if it is actually returning the type, you are expecting, and use your struct reference to cast it to intended specific type. Also ensure that the assembly where the struct is defined (if it's in different assembly) is loaded in memory. view>>.Net assemblies in memory

bp
0 Kudos
Message 7 of 11
(2,199 Views)

Thanks for your effort, as mentioned in original post, I do not know how Matlab treats structs into .NET objects. Understanding this was the intention when creating this post.

 

Mathworks provides a .NET assembly MWArray.dll, which I can load and also call CTORs and obtain a .NET object. 

MWStructArray()

MWStructArray(Int32 rows, Int32 cols, String[] fNames)

MWStructArray(Int32[] inDims, String[] fNames)

MWStructArray(Object[] fieldDefs)

However, I am interested to extract the data from a .NET object.

 

Is there a way to use the self-made .Net assembly and the MWArray.dll to extract data (datatype)? 

 

For the "To More Specific Class": I checked the example. To me it is unclear what a GObject is. Further I don't have a Labview target class, can I create a self-made .lvclass? What would be the "specific class reference" in that case?

0 Kudos
Message 8 of 11
(2,181 Views)

In Matlabs phonebook C# example I found: 

friends = new MWStructArray(2, 2, myFieldNames);
friends["name", 1]= new MWCharArray("Jordan Robert");
friends["phone", 1]= 3386;
friends["name", 2]= new MWCharArray("Mary Smith");
friends["phone", 2]= 3912;
MWStructArray seems to be a multi-dim array.  Is it possible to model a corresponding cluster in Labview? Would this be some cluster in cluster bundle "name" and "phone"?
0 Kudos
Message 9 of 11
(2,178 Views)
Solution
Accepted by topic author maxine_mumann

A GObject is anything on a LabVIEW block diagram or front panel.  The example is misleading because it's oriented towards LabVIEW use, but the same "To more specific class" node can be used to cast .NET references too.

 

In order to do that you do need a wire that is of the correct class.  First, take the object from earlier that you ran "ToString" on, and instead run "GetType" on it, and then from the "GetType" output, run ToString on that.  That will be the class name that you need to make a wire of.  To make the wire, create a .NET constant (just right-click any .NET wire and choose "Create constant"), and then right-click that constant, choose "Select .NET class", and then "Browse".  You will have to point the browse dialog to the DLL containing the class name, then find it in the list and choose it there, and then you can wire that constant to the top input of "To more specific class", the input from your Matlab function into the left side, and then a usable object should be available on the right side output.

Message 10 of 11
(2,156 Views)