依据上面的测试例子,将常用的matlab数据类型转化成C#数据类型方法封装为单独的help类,以便开发人员直接调用该help类。而不用知道matlab相关知识。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using MathWorks.MATLAB.NET.Arrays; namespace MatlabCallCsharpDll.wordHelper { public class WordHelper { /// <summary> /// 将matlab数组矩阵转换成双精度小数数组 /// </summary> /// <param name="matrix">将matlab数组矩阵,如 outMatrix=[1.75,2.68,3.36;4,5.35,6.23];</param> /// <returns>成功:双精度小数数组,错误:null</returns> public static double[,] matrix2DoubleArray(MWArray matrix) { if (matrix!=null) { MWNumericArray tempArray = (MWNumericArray)matrix; double[,] res = (double[,])tempArray.ToArray(); return res; } else { return null; } } /// <summary> /// 将matlab数组矩阵转换成整数数组 /// </summary> /// <param name="matrix">matlab数组矩阵</param> /// <returns>成功:整数数组,失败:null</returns> public static int[,] matrix2IntArray(MWArray matrix) { if (matrix!=null) { MWNumericArray tempArray = (MWNumericArray)matrix; int[,] res = (int[,])tempArray.ToArray(); return res; } else { return null; } } /// <summary> /// matlab字符串矩阵转换成C#字符串 /// </summary> /// <param name="matlabStr">matlab字符串矩阵,如 outString = 'zhang_yang';</param> /// <returns>成功:str;失败:null</returns> public static string MatlabStr2csStr(MWArray matlabStr) { if (matlabStr!=null) { MWCharArray arr = (MWCharArray)matlabStr; string str = arr.ToString(); return str; } else { return null; } } } }