/// <summary> /// 加载double数组类型的数据块 /// </summary> /// <param name="fileData">文件内容</param> /// <param name="dataCols">数组的列数</param> /// <param name="entryName">数据块名称</param> /// <returns>是否成功</returns> public static double[] ReadDoubleArrayData(byte[] fileData, ref int dataCols, string entryName = ACloudSpectrumDataName) { int dataRows = 0; dataCols = 0; IntPtr retptr = IntPtr.Zero; try { if (CommonMethod.Is64BitVersion()) { retptr = MATGetDoubleArrayData64(fileData, fileData.Length, entryName, ref dataRows, ref dataCols); } else { retptr = MATGetDoubleArrayData32(fileData, fileData.Length, entryName, ref dataRows, ref dataCols); } if (retptr == IntPtr.Zero) { ErrorString = FileFormat.GetDLLErrorMessage(); return(null); } return(CommonMethod.CopyDoubleFromIntptrAndFree(ref retptr, dataRows * dataCols)); } catch (Exception ex) { ErrorString = ex.Message; return(null); } }
/// <summary> /// 是否为MAT文件 /// </summary> /// <param name="fileData">文件数据</param> public static bool IsMATFile(byte[] fileData) { if (fileData == null || fileData.Length == 0) { return(false); } bool retcode = false; try { if (CommonMethod.Is64BitVersion()) { retcode = MATIsMATFile64(fileData, fileData.Length); } else { retcode = MATIsMATFile32(fileData, fileData.Length); } if (retcode == false) { ErrorString = FileFormat.GetDLLErrorMessage(); } return(retcode); } catch (Exception ex) { ErrorString = ex.Message; return(false); } }
/// <summary> /// 打开Matlab文件(必须调用MatlabFileClose) /// </summary> /// <param name="fileData">文件内容</param> /// <returns>是否成功</returns> public static bool MatlabFileOpen(byte[] fileData) { if (fileData == null) { ErrorString = "Invalid parameters"; return(false); } try { bool retcode = false; if (CommonMethod.Is64BitVersion()) { retcode = MATFileInitialize64(fileData, fileData.Length); } else { retcode = MATFileInitialize32(fileData, fileData.Length); } if (retcode == false) { ErrorString = FileFormat.GetDLLErrorMessage(); } return(retcode); } catch (Exception ex) { ErrorString = ex.Message; return(false); } }
/// <summary> /// 创建MATLAB文件头 /// </summary> /// <param name="description">文件描述</param> /// <returns>返回BYTE[]</returns> public static byte[] CreateFileHeader(string description) { try { IntPtr retptr = IntPtr.Zero; int datasize = 0; description = "MATLAB 5.0 " + description; //一定要加上MATLAB 5.0才能被再次读出来 if (CommonMethod.Is64BitVersion()) { retptr = MATCreateFileHeader64(description, ref datasize); } else { retptr = MATCreateFileHeader32(description, ref datasize); } if (retptr == IntPtr.Zero) { ErrorString = FileFormat.GetDLLErrorMessage(); return(null); } return(CommonMethod.CopyByteFromIntptrAndFree(ref retptr, datasize)); } catch (Exception ex) { ErrorString = ex.Message; return(null); } }
/// <summary> /// 获取组分信息 /// </summary> /// <param name="fileData"></param> /// <returns></returns> private static List <ComponentInfo> QuantGetComponentInfo(byte[] fileData) { try { IntPtr retptr = IntPtr.Zero; int compCount = 0; if (fileData == null || fileData.Length == 0) { throw new Exception("Invalid Parameter"); } if (CommonMethod.Is64BitVersion()) { retptr = SPAGetComponents64(fileData, fileData.Length, ref compCount); } else { retptr = SPAGetComponents32(fileData, fileData.Length, ref compCount); } if (retptr == IntPtr.Zero) { throw new Exception(FileFormat.GetDLLErrorMessage()); } return(CommonMethod.CopyStructureListFromIntptrAndFree <ComponentInfo>(ref retptr, compCount)); } catch (Exception ex) { ErrorString = ex.Message; return(null); } }
/// <summary> /// 获取模型参数信息 /// </summary> /// <param name="fileData"></param> /// <returns></returns> private static ModelParameter QuantGetModelParameter(byte[] fileData) { try { IntPtr retptr = IntPtr.Zero; if (fileData == null || fileData.Length == 0) { throw new Exception("Invalid Parameter"); } if (CommonMethod.Is64BitVersion()) { retptr = SPAGetModelParameter64(fileData, fileData.Length); } else { retptr = SPAGetModelParameter32(fileData, fileData.Length); } if (retptr == IntPtr.Zero) { throw new Exception(FileFormat.GetDLLErrorMessage()); } bool retOK = true; var retData = CommonMethod.CopyStructureFromIntptrAndFree <ModelParameter>(ref retptr, out retOK); if (!retOK) { ErrorString = "Invalid data reading"; retData.allFileCount = int.MaxValue; //错误 } return(retData); } catch (Exception ex) { ErrorString = ex.Message; var retData = new ModelParameter(); retData.allFileCount = int.MaxValue; return(retData); } }
/// <summary> /// 创建double类型的数据块(不压缩格式) /// </summary> /// <param name="fileData">数据</param> /// <param name="dataRows">数据行数</param> /// <param name="entryName">数据块名称</param> /// <returns>Matlab数据块</returns> public static byte[] CreateDoubleEntry(double[] fileData, int dataRows, string entryName = ACloudSpectrumDataName) { if (fileData == null || fileData.Length < 0 || (fileData.Length % dataRows) != 0 || string.IsNullOrWhiteSpace(entryName)) { ErrorString = "Invalid parameters"; return(null); } try { int dataCols = fileData.Length / dataRows; IntPtr retptr = IntPtr.Zero; int datasize = 0; if (CommonMethod.Is64BitVersion()) { retptr = MATCreateDoubleEntry64(fileData, fileData.Length, entryName, dataRows, dataCols, ref datasize); } else { retptr = MATCreateDoubleEntry32(fileData, fileData.Length, entryName, dataRows, dataCols, ref datasize); } if (retptr == IntPtr.Zero) { ErrorString = FileFormat.GetDLLErrorMessage(); return(null); } return(CommonMethod.CopyByteFromIntptrAndFree(ref retptr, datasize)); } catch (Exception ex) { ErrorString = ex.Message; return(null); } }