上上篇的博客已经提到如何配置环境,即如何在asp.net中调用matlab生成的dll文件。这篇博客打算做个笔记,那就是matlab和C#数据类型如何转换。随着需求的增加,我会不断增加新的类型转换。
(1)C#二维数组转为Matlab矩阵
//这段代码主要就是将C#的二维数组转为Matlab的矩阵 double[,] frontDrawDataArray = new double[250, 290]; double[,] sideDrawDataArray = new double[250, 290]; double[,] topDrawDataArray = new double[250, 290]; for (int i = 0; i < 250; i++) { for (int j = 0; j < 290; j++) { frontDrawDataArray[i, j] = double.Parse(frontDrawDataStrArray[i * 290 + j]); sideDrawDataArray[i, j] = double.Parse(sideDrawDataStrArray[i * 290 + j]); topDrawDataArray[i, j] = double.Parse(topDrawDataStrArray[i * 290 + j]); } } MWNumericArray frontSket = new MWNumericArray(frontDrawDataArray); MWNumericArray sideSket = new MWNumericArray(sideDrawDataArray); MWNumericArray topSket = new MWNumericArray(topDrawDataArray);
(2)Matlab的字符cell array转为C#的字符串
//C#访问matlab返回的字符cell array //res在C#是MWArray类型,在Matlab中它是个cell array(res = cell(1,N)),里面的每一个元素是字符串。 MWCellArray nameCellArray = (MWCellArray)res; string sortedModelNames = string.Empty; int model_num = nameCellArray.NumberOfElements; for (int i = 1; i <= model_num; i++) { sortedModelNames += nameCellArray[i].ToString(); //访问cell array的第i个元素 sortedModelNames += ‘,‘; } sortedModelNames += nameCellArray[model_num].ToString();
(3) Matlab数组转为C#的数组
double[,] csArray= (double[,])result.ToArray
时间: 2024-10-26 11:08:39