Hi,
To my understanding, you initially had a MATLAB DLL function providing an MWArray output named “Result” convertible to MWStructArray in C#. The function was later modified to yield “Result_JSon”, an MWCharArray which you obtained through JSON encoding. You want to convert the MWCharArray back into MWStructArray format in C#.
You can follow below mentioned steps for the proper conversion:
- Update MATLAB Function: Make sure your MATLAB function now generates a JSON string(‘Result_JSon’) using the ‘jsonencode’ function.
- Install Newtonsoft.Json: In your C# project, install the Newtonsoft.Json library using Nuget Package Manager.
- De-serialize JSON String: Deserialize the JSON string(‘Result_JSon’) into a C# object using a nifty tool from Newtonsoft.Json.
- Convert to MWStructArray: Create a function to convert fields of the C# object to correct MWArray types and field names.
using MathWorks.MATLAB.NET.Arrays;
using Newtonsoft.Json;
MWArray Result;
MWCharArray Result_JSon = (MWCharArray)yourMatlabFunctionCall();
// Step 1: Convert MWCharArray to C# string
string jsonString = Result_JSon.ToString();
// Step 2: Deserialize JSON string into C# object
YourCSharpObjectType resultObject = JsonConvert.DeserializeObject<YourCSharpObjectType>(jsonString);
// Step 3: Convert C# object to MWStructArray
MWStructArray Results_Bis = ConvertToMWStructArray(resultObject);
// Your ConvertToMWStructArray function
MWStructArray ConvertToMWStructArray(YourCSharpObjectType resultObject)
{
// Create a new MWStructArray
MWStructArray mwStructArray = new MWStructArray(1, 1);
// Customize the following based on your actual field names
// For each field, convert the C# value to the appropriate MWArray type
mwStructArray["FieldName1"] = new MWNumericArray(resultObject.Field1);
mwStructArray["FieldName2"] = new MWCharArray(resultObject.Field2);
// ...
return mwStructArray;
}
You can find more information in the following link:
Hope it helps!
Regards,
Soumanth