C# MATLAB Integration and MWArray Conversion
조회 수: 11 (최근 30일)
이전 댓글 표시
I am attempting to integrate code that was written in MATLAB into a C# program. Originally the C# code called the installed MATLAB program that was on the users computer in order to compute a result. This is to be changed so that the user does not need MATLAB installed on their computer and can simply run the C# program. The MATLAB code was compiled using MATLAb's built in .NET assembly compiler and the the .NET dll were interfaced into C#. The user would then only need to install the MATLAB runtime to use the program.
In the old version the output of the MATLAB program call was put into an object array. The object array was then converted to a double [,] array or an object[,] array and assigned to a DataRow based on an index. Some of the old code is below:
object result = null;
matlab.Feval("GetReadRates", 9, out result, str_Test_Type, str_Symbol_Set, str_Test_Name, str_Test_Scanner, str_Test_Speed, str_Test_Angle, str_Test_Distance, str_Test_Number,numScans, speedThreshold, noReadString, str_DataFilepath);
object[] res = result as object[];
newSymbolDataRow["Read_Rate"] = (res[0] as double[,])[0, symbolIndex];
newSymbolDataRow["NoRead_Rate"] = (res[1] as double[,])[0, symbIndex];
newSymbolDataRow["Misread_Rate"] = (res[2] as double[,])[0, symbolIndex];
newSymbolDataRow["Misreads"] = (res[3] as object[,])[0, symbolIndex];
In the new version the output of the MATLAB class has to be stored in a MWArray. From this MWArray the data has to be converted and assigned to a DataRow just as the old code did.
The way I would like to approach this is to converted the MWArray to an object array and use the same code for the DataRows. However, this does not seem possible and very few methods available for converting data. The MWArray class has MWArray.GetValue(int index), MWArray.toString(), and MWArray.toArray(). The MWArray.toArray() I have attempted to use as seen below.
MWArray str_Test_TypeMW = str_Test_Type;
MWArray str_Symbol_SetMW = str_Symbol_Set;
MWArray numScansMW = numScans;
MWArray speedThresholdMW = speedThreshold;
MWArray noReadStringMW = noReadString;
MWArray str_DataFilepathMW = str_DataFilepath;
MWArray str_Test_NameMW = str_Test_Name;
MWArray str_Test_ScannerMW = str_Test_Scanner;
MWArray str_Test_SpeedMW = str_Test_Speed;
MWArray str_Test_AngleMW = str_Test_Angle;
MWArray str_Test_DistanceMW = str_Test_Distance;
MWArray str_Test_NumberMW = str_Test_Number;
MWArray [] OutResult = null;
GRRClass matlabreplacement;
matlabreplacement = new GRRClass();
OutResult=matlabreplacement.GetReadRates(9, str_Test_TypeMW, str_Symbol_SetMW, str_Test_NameMW, str_Test_ScannerMW, str_Test_SpeedMW, str_Test_AngleMW, str_Test_DistanceMW, str_Test_NumberMW,numScansMW, speedThresholdMW, noReadStringMW, str_DataFilepathMW);
Array resArray = Array.CreateInstance(typeof(object), 9, 2);
resArray.Initialize();
resArray = OutResult.ToArray();
newSymbolDataRow["Read_Rate"] = (resArray.GetValue(0) as double[,])[0, symbolIndex];
The MWArray OutResult has this format according to Visual Studio:
OutResult {MathWorks.MATLAB.NET.Arrays.MWArray[9]} MathWorks.MATLAB.NET.Arrays.MWArray[]
[0] {0 0} MathWorks.MATLAB.NET.Arrays.MWArray {MathWorks.MATLAB.NET.Arrays.MWNumericArray}
[1] {0 0} MathWorks.MATLAB.NET.Arrays.MWArray {MathWorks.MATLAB.NET.Arrays.MWNumericArray}
[2] {100 100} MathWorks.MATLAB.NET.Arrays.MWArray {MathWorks.MATLAB.NET.Arrays.MWNumericArray}
[3] {'10-000-001(2)' '10-000-002(2)'} MathWorks.MATLAB.NET.Arrays.MWArray {MathWorks.MATLAB.NET.Arrays.MWCellArray}
[4] {0.0150 0.0250} MathWorks.MATLAB.NET.Arrays.MWArray {MathWorks.MATLAB.NET.Arrays.MWNumericArray}
[5] {0 0} MathWorks.MATLAB.NET.Arrays.MWArray {MathWorks.MATLAB.NET.Arrays.MWNumericArray}
[6] {0.6205 0.6205} MathWorks.MATLAB.NET.Arrays.MWArray {MathWorks.MATLAB.NET.Arrays.MWNumericArray}
[7] {0 0} MathWorks.MATLAB.NET.Arrays.MWArray {MathWorks.MATLAB.NET.Arrays.MWNumericArray}
[8] {Analysis Completed} MathWorks.MATLAB.NET.Arrays.MWArray {MathWorks.MATLAB.NET.Arrays.MWCharArray}
With each element having indices for the two values. The Array resArray has the same format as the MWArray.
Whenever I attempt to run the program I get an exception "Object reference not set to an instance of an object" whenever the linThank you for any help, I am still a beginner at C# programming. e "newSymbolDataRow" runs.
댓글 수: 0
답변 (0개)
참고 항목
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!