private static MLCell CreateCellArrayCustom(string name, string[] items) { var cell = new MLCell(name, new int[] { items.Length, 1 }); int i = 0; foreach (var item in items) { double ans = 0; if (double.TryParse(items[i], out ans)) { cell[i] = new MLDouble(null, new double[] { ans }, 1); i++; continue; } if (items[i].TrimStart().StartsWith("[") && items[i].TrimEnd().EndsWith("]")) { cell[i] = CreateCellArrayCustom("", items[i].Split(new[] { ' ', '[', ']' }, StringSplitOptions.RemoveEmptyEntries)); i++; continue; } cell[i] = new MLChar(null, (string)items[i].Trim()); i++; } return(cell); }
static void DoWrite(string fn, Dictionary <string, MatLab.DoubleList> data, Dictionary <string, List <MLCell> > dataCell, SortedDictionary <string, double> param, List <MLArray> mlList, Hashtable seen) { log.Info("DoWrite start " + (GC.GetTotalMemory(false) / 1024.0 / 1024.0)); foreach (var item in data) { double[][] temp = item.Value.ToArray(); MLArray dbarray = new MLDouble(item.Key, temp); mlList.Add(dbarray); log.Info("DoWrite Double " + item.Key + " " + (GC.GetTotalMemory(false) / 1024.0 / 1024.0)); } // datacell contains rows foreach (var item in dataCell) { // create msg table MLCell temp1 = new MLCell(item.Key + "1", new int[] { 1, item.Value.Count }); int a = 0; // add rows to msg table foreach (var mlCell in item.Value) { temp1[a] = item.Value[a]; a++; } // add table to masterlist mlList.Add(temp1); log.Info("DoWrite Cell " + item.Key + " " + (GC.GetTotalMemory(false) / 1024.0 / 1024.0)); } log.Info("DoWrite mllist " + (GC.GetTotalMemory(false) / 1024.0 / 1024.0)); MLCell cell = new MLCell("PARM", new int[] { param.Keys.Count, 2 }); int m = 0; foreach (var item in param.Keys) { cell[m, 0] = new MLChar(null, item.ToString()); cell[m, 1] = new MLDouble(null, new double[] { (double)param[item] }, 1); m++; } mlList.Add(cell); MLArray seenmsg = CreateCellArray("Seen", seen.Keys.Cast <string>().ToArray()); mlList.Add(seenmsg); try { log.Info("write " + fn + ".mat"); log.Info("DoWrite before" + (GC.GetTotalMemory(false) / 1024.0 / 1024.0)); MatFileWriter mfw = new MatFileWriter(fn + ".mat", mlList, false); log.Info("DoWrite done" + (GC.GetTotalMemory(false) / 1024.0 / 1024.0)); } catch (Exception err) { throw new Exception("There was an error when creating the MAT-file: \n" + err.ToString(), err); } }
public WiderFaces(string pathImages, string pathMat) { WiderFaces._pathMat = pathMat; this._pathImages = pathImages; var mat = new MatFileReader(_pathMat); _allFiles = mat.Content["file_list"] as MLCell; _allBoxes = mat.Content["face_bbx_list"] as MLCell; }
private static MLArray CreateCellArray(string name, string[] names) { MLCell cell = new MLCell(name, new int[] { names.Length, 1 }); for (int i = 0; i < names.Length; i++) { cell[i] = new MLChar(null, names[i]); } return(cell); }
static MLArray CreateCellArray() { var names = new[] { "Hello", "World", "I am", "a", "MAT-file" }; var cell = new MLCell("Names", new[] { names.Length, 1 }); for (var i = 0; i < names.Length; i++) { cell[i] = new MLChar(null, names[i]); } return(cell); }
private static MLArray CreateCellArray() { string[] names = new string[] { "Hello", "World", "I am", "a", "MAT-file" }; MLCell cell = new MLCell("Names", new int[] { names.Length, 1 }); for (int i = 0; i < names.Length; i++) { cell[i] = new MLChar(null, names[i]); } return(cell); }
static void DoWrite(string fn, Dictionary <string, DoubleList> data, SortedDictionary <string, double> param, List <MLArray> mlList, Hashtable seen) { log.Info("DoWrite start " + (GC.GetTotalMemory(false) / 1024.0 / 1024.0)); foreach (var item in data) { double[][] temp = item.Value.ToArray(); MLArray dbarray = new MLDouble(item.Key, temp); mlList.Add(dbarray); log.Info("DoWrite " + item.Key + " " + (GC.GetTotalMemory(false) / 1024.0 / 1024.0)); } log.Info("DoWrite mllist " + (GC.GetTotalMemory(false) / 1024.0 / 1024.0)); MLCell cell = new MLCell("PARM", new int[] { param.Keys.Count, 2 }); int m = 0; foreach (var item in param.Keys) { cell[m, 0] = new MLChar(null, item.ToString()); cell[m, 1] = new MLDouble(null, new double[] { (double)param[item] }, 1); m++; } mlList.Add(cell); MLArray seenmsg = CreateCellArray("Seen", seen.Keys.Cast <string>().ToArray()); mlList.Add(seenmsg); try { log.Info("write " + fn + ".mat"); log.Info("DoWrite before" + (GC.GetTotalMemory(false) / 1024.0 / 1024.0)); MatFileWriter mfw = new MatFileWriter(fn + ".mat", mlList, true); log.Info("DoWrite done" + (GC.GetTotalMemory(false) / 1024.0 / 1024.0)); } catch (Exception err) { CustomMessageBox.Show("There was an error when creating the MAT-file: \n" + err.ToString(), "MAT-File Creation Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } }
private static MLCell CreateCellArrayCustom(string name, string[] items) { var cell = new MLCell(name, new int[] { items.Length, 1 }); int i = 0; foreach (var item in items) { double ans = 0; if (double.TryParse(items[i], out ans)) { cell[i] = new MLDouble(null, new double[] { ans }, 1); i++; continue; } cell[i] = new MLChar(null, (string)items[i].Trim()); i++; } return(cell); }
/// <summary> /// Reads <c>miMATRIX</c> from the input stream. /// </summary> /// <remarks> /// If reading was not finished (which is normal for filtered results) /// returns <c>null</c>. /// </remarks> /// <param name="buf">The <c>BinaryReader</c> input stream.</param> /// <param name="isRoot">When <c>true</c> informs that if this is a top level /// matrix.</param> /// <returns><c>MLArray</c> or <c>null</c> if matrix does not match <c>filter</c></returns> private MLArray ReadMatrix(Stream buf, bool isRoot) { // result MLArray mlArray = null; ISMatTag tag; // read flags int[] flags = ReadFlags(buf); int attributes = (flags.Length != 0) ? flags[0] : 0; int nzmax = (flags.Length != 0) ? flags[1] : 0; int type = attributes & 0xff; // read Array dimension int[] dims = ReadDimension(buf); // read Array name string name = ReadName(buf); // If this array is filtered out return immediately if (isRoot && !_filter.Matches(name)) { return(null); } // read data switch (type) { case MLArray.mxSTRUCT_CLASS: MLStructure mlStruct = new MLStructure(name, dims, type, attributes); BinaryReader br = new BinaryReader(buf); // field name length - this subelement always uses the compressed data element format tag = new ISMatTag(br.BaseStream); int maxlen = br.ReadInt32(); // Read fields data as Int8 tag = new ISMatTag(br.BaseStream); // calculate number of fields int numOfFields = tag.Size / maxlen; // padding after field names int padding = (tag.Size % 8) != 0 ? 8 - tag.Size % 8 : 0; string[] fieldNames = new string[numOfFields]; for (int i = 0; i < numOfFields; i++) { byte[] names = new byte[maxlen]; br.Read(names, 0, names.Length); fieldNames[i] = ZeroEndByteArrayToString(names); } br.ReadBytes(padding); // read fields for (int index = 0; index < mlStruct.M * mlStruct.N; index++) { for (int i = 0; i < numOfFields; i++) { // read matrix recursively tag = new ISMatTag(br.BaseStream); if (tag.Size > 0) { MLArray fieldValue = ReadMatrix(br.BaseStream, false); mlStruct[fieldNames[i], index] = fieldValue; } else { mlStruct[fieldNames[i], index] = new MLEmptyArray(); } } } mlArray = mlStruct; //br.Close(); break; case MLArray.mxCELL_CLASS: MLCell cell = new MLCell(name, dims, type, attributes); for (int i = 0; i < cell.M * cell.N; i++) { tag = new ISMatTag(buf); if (tag.Size > 0) { MLArray cellmatrix = ReadMatrix(buf, false); cell[i] = cellmatrix; } else { cell[i] = new MLEmptyArray(); } } mlArray = cell; break; case MLArray.mxDOUBLE_CLASS: mlArray = new MLDouble(name, dims, type, attributes); //read real tag = new ISMatTag(buf); tag.ReadToByteBuffer(((MLNumericArray <double>)mlArray).RealByteBuffer, (IByteStorageSupport)mlArray); // read complex if (mlArray.IsComplex) { tag = new ISMatTag(buf); tag.ReadToByteBuffer(((MLNumericArray <double>)mlArray).ImaginaryByteBuffer, (IByteStorageSupport)mlArray); } break; case MLArray.mxSINGLE_CLASS: mlArray = new MLSingle(name, dims, type, attributes); //read real tag = new ISMatTag(buf); tag.ReadToByteBuffer(((MLNumericArray <float>)mlArray).RealByteBuffer, (IByteStorageSupport)mlArray); // read complex if (mlArray.IsComplex) { tag = new ISMatTag(buf); tag.ReadToByteBuffer(((MLNumericArray <float>)mlArray).ImaginaryByteBuffer, (IByteStorageSupport)mlArray); } break; case MLArray.mxUINT8_CLASS: mlArray = new MLUInt8(name, dims, type, attributes); //read real tag = new ISMatTag(buf); tag.ReadToByteBuffer(((MLNumericArray <byte>)mlArray).RealByteBuffer, (IByteStorageSupport)mlArray); // read complex if (mlArray.IsComplex) { tag = new ISMatTag(buf); tag.ReadToByteBuffer(((MLNumericArray <byte>)mlArray).ImaginaryByteBuffer, (IByteStorageSupport)mlArray); } break; case MLArray.mxINT8_CLASS: mlArray = new MLInt8(name, dims, type, attributes); //read real tag = new ISMatTag(buf); tag.ReadToByteBuffer(((MLNumericArray <sbyte>)mlArray).RealByteBuffer, (IByteStorageSupport)mlArray); // read complex if (mlArray.IsComplex) { tag = new ISMatTag(buf); tag.ReadToByteBuffer(((MLNumericArray <sbyte>)mlArray).ImaginaryByteBuffer, (IByteStorageSupport)mlArray); } break; case MLArray.mxUINT16_CLASS: mlArray = new MLUInt16(name, dims, type, attributes); //read real tag = new ISMatTag(buf); tag.ReadToByteBuffer(((MLNumericArray <ushort>)mlArray).RealByteBuffer, (IByteStorageSupport)mlArray); // read complex if (mlArray.IsComplex) { tag = new ISMatTag(buf); tag.ReadToByteBuffer(((MLNumericArray <ushort>)mlArray).ImaginaryByteBuffer, (IByteStorageSupport)mlArray); } break; case MLArray.mxINT16_CLASS: mlArray = new MLInt16(name, dims, type, attributes); //read real tag = new ISMatTag(buf); tag.ReadToByteBuffer(((MLNumericArray <short>)mlArray).RealByteBuffer, (IByteStorageSupport)mlArray); // read complex if (mlArray.IsComplex) { tag = new ISMatTag(buf); tag.ReadToByteBuffer(((MLNumericArray <short>)mlArray).ImaginaryByteBuffer, (IByteStorageSupport)mlArray); } break; case MLArray.mxUINT32_CLASS: mlArray = new MLUInt32(name, dims, type, attributes); //read real tag = new ISMatTag(buf); tag.ReadToByteBuffer(((MLNumericArray <uint>)mlArray).RealByteBuffer, (IByteStorageSupport)mlArray); // read complex if (mlArray.IsComplex) { tag = new ISMatTag(buf); tag.ReadToByteBuffer(((MLNumericArray <uint>)mlArray).ImaginaryByteBuffer, (IByteStorageSupport)mlArray); } break; case MLArray.mxINT32_CLASS: mlArray = new MLInt32(name, dims, type, attributes); //read real tag = new ISMatTag(buf); tag.ReadToByteBuffer(((MLNumericArray <int>)mlArray).RealByteBuffer, (IByteStorageSupport)mlArray); // read complex if (mlArray.IsComplex) { tag = new ISMatTag(buf); tag.ReadToByteBuffer(((MLNumericArray <int>)mlArray).ImaginaryByteBuffer, (IByteStorageSupport)mlArray); } break; case MLArray.mxUINT64_CLASS: mlArray = new MLUInt64(name, dims, type, attributes); //read real tag = new ISMatTag(buf); tag.ReadToByteBuffer(((MLNumericArray <ulong>)mlArray).RealByteBuffer, (IByteStorageSupport)mlArray); // read complex if (mlArray.IsComplex) { tag = new ISMatTag(buf); tag.ReadToByteBuffer(((MLNumericArray <ulong>)mlArray).ImaginaryByteBuffer, (IByteStorageSupport)mlArray); } break; case MLArray.mxINT64_CLASS: mlArray = new MLInt64(name, dims, type, attributes); //read real tag = new ISMatTag(buf); tag.ReadToByteBuffer(((MLNumericArray <long>)mlArray).RealByteBuffer, (IByteStorageSupport)mlArray); // read complex if (mlArray.IsComplex) { tag = new ISMatTag(buf); tag.ReadToByteBuffer(((MLNumericArray <long>)mlArray).ImaginaryByteBuffer, (IByteStorageSupport)mlArray); } break; case MLArray.mxCHAR_CLASS: MLChar mlchar = new MLChar(name, dims, type, attributes); //read real tag = new ISMatTag(buf); char[] ac = tag.ReadToCharArray(); for (int i = 0; i < ac.Length; i++) { mlchar.SetChar(ac[i], i); } mlArray = mlchar; break; case MLArray.mxSPARSE_CLASS: MLSparse sparse = new MLSparse(name, dims, attributes, nzmax); // read ir (row indices) tag = new ISMatTag(buf); int[] ir = tag.ReadToIntArray(); // read jc (column indices) tag = new ISMatTag(buf); int[] jc = tag.ReadToIntArray(); // read pr (real part) tag = new ISMatTag(buf); double[] ad1 = tag.ReadToDoubleArray(); int n = 0; for (int i = 0; i < ir.Length; i++) { if (i < sparse.N) { n = jc[i]; } sparse.SetReal(ad1[i], ir[i], n); } //read pi (imaginary part) if (sparse.IsComplex) { tag = new ISMatTag(buf); double[] ad2 = tag.ReadToDoubleArray(); int n1 = 0; for (int i = 0; i < ir.Length; i++) { if (i < sparse.N) { n1 = jc[i]; } sparse.SetImaginary(ad2[i], ir[i], n1); } } mlArray = sparse; break; default: throw new MatlabIOException("Incorrect Matlab array class: " + MLArray.TypeToString(type)); } return(mlArray); }
public static void log(string fn) { string[] filelines; if (fn.ToLower().EndsWith(".bin")) { filelines = BinaryLog.ReadLog(fn).ToArray(); } else { // read the file filelines = File.ReadAllLines(fn); } // store all the arrays List <MLArray> mlList = new List <MLArray>(); // store data to putinto the arrays Dictionary <string, List <double[]> > data = new Dictionary <string, List <double[]> >(); // store line item lengths Hashtable len = new Hashtable(); // store whats we have seen in the log Hashtable seen = new Hashtable(); // store the params seen SortedDictionary <string, double> param = new SortedDictionary <string, double>(); // keep track of line no int a = 0; foreach (var line in filelines) { a++; Console.Write(a + "\r"); string strLine = line.Replace(", ", ","); strLine = strLine.Replace(": ", ":"); string[] items = strLine.Split(',', ':'); // process the fmt messages if (line.StartsWith("FMT")) { // +1 for line no string[] names = new string[items.Length - 5 + 1]; names[0] = "LineNo"; Array.ConstrainedCopy(items, 5, names, 1, names.Length - 1); MLArray format = CreateCellArray(items[3] + "_label", names); if (items[3] == "PARM") { } else { mlList.Add(format); } len[items[3]] = names.Length; } // process param messages else if (line.StartsWith("PARM")) { try { param[items[1]] = double.Parse(items[2], CultureInfo.InvariantCulture); } catch { } }// everyting else is generic else { // make sure the line is long enough if (items.Length < 2) { continue; } // check we have a valid fmt message for this message type if (!len.ContainsKey(items[0])) { continue; } // check the fmt length matchs what the log has if (items.Length != (int)len[items[0]]) { continue; } // make it as being seen seen[items[0]] = 1; double[] dbarray = new double[items.Length]; // set line no dbarray[0] = a; for (int n = 1; n < items.Length; n++) { try { dbarray[n] = double.Parse(items[n], CultureInfo.InvariantCulture); } catch { } } if (!data.ContainsKey(items[0])) { data[items[0]] = new List <double[]>(); } data[items[0]].Add(dbarray); } } foreach (var item in data) { double[][] temp = item.Value.ToArray(); MLArray dbarray = new MLDouble(item.Key, temp); mlList.Add(dbarray); } MLCell cell = new MLCell("PARM", new int[] { param.Keys.Count, 2 }); int m = 0; foreach (var item in param.Keys) { cell[m, 0] = new MLChar(null, item.ToString()); cell[m, 1] = new MLDouble(null, new double[] { (double)param[item] }, 1); m++; } mlList.Add(cell); MLArray seenmsg = CreateCellArray("Seen", seen.Keys.Cast <string>().ToArray()); mlList.Add(seenmsg); try { MatFileWriter mfw = new MatFileWriter(fn + ".mat", mlList, true); } catch (Exception err) { MessageBox.Show("There was an error when creating the MAT-file: \n" + err.ToString(), "MAT-File Creation Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } }
public static void writeDOTnirs(core.Data data, string filename, int startIdx = 0, int endIdx = Int32.MaxValue) { // Store the data into the *.nirs matlab format try { int numsamples = data.numsamples; int numch = data.probe.numChannels; int naux = data.auxillaries.Length; numsamples = Math.Min(endIdx - startIdx, numsamples - startIdx); // save the structure as mat file using MatFileWriter List <MLArray> mlList = new List <MLArray>(); double[][] d = new double[numsamples][]; double[][] t = new double[numsamples][]; double[][] aux = new double[numsamples][]; for (int j = startIdx; j < startIdx + numsamples; j++) { double[] dloc = new double[numch]; for (int i = 0; i < numch; i++) { dloc[i] = data.data[i][j]; } try { if (naux > 0) { double[] aloc = new double[naux]; for (int ii = 0; ii < naux; ii++) { aloc[ii] = data.auxillaries[ii].data[j]; } aux[j - startIdx] = aloc; } else { double[] aa = new double[1]; aa[0] = 0; aux[j - startIdx] = aa; } } catch { // Console.WriteLine("error writing aux"); double[] aa = new double[1]; aa[0] = 0; aux[j - startIdx] = aa; } double[] tt = new double[1]; double[] ss = new double[1]; ss[0] = 0; tt[0] = data.time[j]; t[j - startIdx] = tt; d[j - startIdx] = dloc; } MLDouble mldata = new MLDouble("d", d); mlList.Add(mldata); MLDouble mlaux = new MLDouble("aux", aux); mlList.Add(mlaux); MLDouble mltime = new MLDouble("t", t); mlList.Add(mltime); double[][] s = new double[numsamples][]; for (int j = startIdx; j < startIdx + numsamples; j++) { double[] dloc = new double[data.stimulus.Count]; double thistime = data.time[j]; for (int i = 0; i < data.stimulus.Count; i++) { dloc[i] = 0; for (int k = 0; k < data.stimulus[i].onsets.Count; k++) { double onset = data.stimulus[i].onsets[k]; double dur = data.stimulus[i].duration[k]; if (thistime >= onset & thistime <= onset + dur) { dloc[i] = data.stimulus[i].amplitude[k]; } } } s[j - startIdx] = dloc; } MLDouble mls = new MLDouble("s", s); mlList.Add(mls); MLCell condNames = new MLCell("CondNames", new int[] { 1, data.stimulus.Count }); for (int i = 0; i < data.stimulus.Count; i++) { condNames[0, i] = new MLChar(null, data.stimulus[i].name); } mlList.Add(condNames); // Probe MLStructure mlSD = new MLStructure("SD", new int[] { 1, 1 }); List <string> datasubtype = new List <string>(); for (int i = 0; i < data.probe.numChannels; i++) { if (!datasubtype.Contains(data.probe.ChannelMap[i].datasubtype)) { datasubtype.Add(data.probe.ChannelMap[i].datasubtype); } } double[] lambda = new double[datasubtype.Count]; for (int i = 0; i < data.probe.numChannels; i++) { lambda[datasubtype.IndexOf(data.probe.ChannelMap[i].datasubtype)] = data.probe.ChannelMap[i].wavelength; } double[][] srcpos = new double[data.probe.numSrc][]; for (int j = 0; j < data.probe.numSrc; j++) { double[] slo = new double[3]; for (int i = 0; i < 2; i++) { slo[i] = data.probe.SrcPos[j, i]; } slo[2] = 0; srcpos[j] = slo; } double[][] detpos = new double[data.probe.numDet][]; for (int j = 0; j < data.probe.numDet; j++) { double[] dlo = new double[3]; for (int i = 0; i < 2; i++) { dlo[i] = data.probe.DetPos[j, i]; } dlo[2] = 0; detpos[j] = dlo; } mlSD["NumDet", 0] = new MLDouble("", new double[] { data.probe.numDet }, 1); mlSD["NumSrc", 0] = new MLDouble("", new double[] { data.probe.numSrc }, 1); mlSD["Lambda", 0] = new MLDouble("", lambda, 1); mlSD["SrcPos", 0] = new MLDouble("", srcpos); mlSD["DetPos", 0] = new MLDouble("", detpos); if (data.probe.isregistered) { double[][] srcpos3d = new double[data.probe.numSrc][]; for (int j = 0; j < data.probe.numSrc; j++) { double[] slo = new double[3]; for (int i = 0; i < 3; i++) { slo[i] = data.probe.SrcPos3D[j, i]; } srcpos3d[j] = slo; } double[][] detpos3d = new double[data.probe.numDet][]; for (int j = 0; j < data.probe.numDet; j++) { double[] dlo = new double[3]; for (int i = 0; i < 3; i++) { dlo[i] = data.probe.DetPos3D[j, i]; } detpos3d[j] = dlo; } mlSD["SrcPos3D", 0] = new MLDouble("", srcpos3d); mlSD["DetPos3D", 0] = new MLDouble("", detpos3d); } // fixes for HOMER2 mlSD["SpatialUnit"] = new MLChar("", "mm"); // Add demographics as a struct MLStructure demo = new MLStructure("demographics", new int[] { 1, 1 }); for (int i = 0; i < data.demographics.Keys.Count; i++) { object val = data.demographics.get(data.demographics.Keys[i]); string valstr = string.Format("{0}", val); demo[data.demographics.Keys[i], 0] = new MLChar("", valstr); } mlList.Add(demo); double[][] ml = new double[data.probe.numChannels][]; for (int i = 0; i < data.probe.numChannels; i++) { double[] m = new double[4]; m[0] = data.probe.ChannelMap[i].sourceindex + 1; m[1] = data.probe.ChannelMap[i].detectorindex + 1; m[2] = 0; m[3] = 1 + datasubtype.IndexOf(data.probe.ChannelMap[i].datasubtype); ml[i] = m; } MLDouble mlml = new MLDouble("ml", ml); mlList.Add(mlml); mlSD["MeasList", 0] = new MLDouble("", ml); mlList.Add(mlSD); MLStructure mlStim = new MLStructure("StimDesign", new int[] { data.stimulus.Count, 1 }); for (int i = 0; i < data.stimulus.Count; i++) { mlStim["name", i] = new MLChar("", data.stimulus[i].name); double[] onset = new double[data.stimulus[i].onsets.Count]; for (int ii = 0; ii < data.stimulus[i].onsets.Count; ii++) { onset[ii] = data.stimulus[i].onsets[ii]; } double[] dur = new double[data.stimulus[i].duration.Count]; for (int ii = 0; ii < data.stimulus[i].duration.Count; ii++) { dur[ii] = data.stimulus[i].duration[ii]; } double[] amp = new double[data.stimulus[i].amplitude.Count]; for (int ii = 0; ii < data.stimulus[i].amplitude.Count; ii++) { amp[ii] = data.stimulus[i].amplitude[ii]; } mlStim["onset", i] = new MLDouble("", onset, 1); mlStim["dur", i] = new MLDouble("", dur, 1); mlStim["amp", i] = new MLDouble("", amp, 1); } if (data.stimulus.Count > 0) { mlList.Add(mlStim); } new MatFileWriter(filename, mlList, false); } catch { Console.WriteLine("Unable to save .nirs file"); } return; }
public async static Task Export(String filename, Project project, ILogger logger = null, CancellationToken cancelletionToken = default(CancellationToken)) { logger?.WriteLineInfo(""); logger?.WriteLineInfo("Saving project into MAT file!"); logger?.WriteLineInfo("MAT file location: " + filename); List <MLArray> mlList = new List <MLArray>(); int pi = 0; foreach (DataStream dataStream in project.DataStreams) { pi++; cancelletionToken.ThrowIfCancellationRequested(); logger?.WriteLineInfo("Exporting " + dataStream.ShortName + " datastream..."); logger?.WriteProgress((double)pi / (double)project.DataStreams.Count() * 100.0); MLStructure structDataStream = new MLStructure(dataStream.ShortName, new int[] { 1, 1 }); structDataStream["Name", 0] = new MLChar(null, dataStream.Name); structDataStream["ShortName", 0] = new MLChar(null, dataStream.ShortName); if (dataStream.DataLines.Count > 0) { // DataLines MLCell arrayDataLines = new MLCell("DataLines", new int[] { dataStream.DataLines.Count, 1 }); int k = 0; foreach (DataLine dataLine in dataStream.DataLines) { MLStructure structDataLine = new MLStructure(dataStream.ShortName, new int[] { 1, 1 }); structDataLine["Timestamp", 0] = new MLDouble("", new double[] { Utils.ConvertToUnixTimestamp(dataLine.TimeStamp) }, 1); structDataLine["TimestampC", 0] = ConvertDateTimeToMLDouble(dataLine.TimeStamp); if (dataLine is ImageDataLine) { structDataLine["ImageFileName", 0] = new MLChar(null, (dataLine as ImageDataLine).ImageFileName); } if (dataLine is VideoDataLine) { structDataLine["VideoFileName", 0] = new MLChar(null, (dataLine as VideoDataLine).VideoFileName); } arrayDataLines[k] = structDataLine; k++; } structDataStream["DataLines", 0] = arrayDataLines; } if (dataStream is GPSDataStream) { GPSDataStream gpsDataStream = dataStream as GPSDataStream; // Event Marker if (gpsDataStream.MarkerEvents.Count > 0) { MLCell arrayMarkerEvents = new MLCell("MarkerEvents", new int[] { gpsDataStream.MarkerEvents.Count, 1 }); int k = 0; foreach (EvenMarkerDataLine evnt in gpsDataStream.MarkerEvents) { MLStructure structEventMarkers = new MLStructure(dataStream.ShortName, new int[] { 1, 1 }); structEventMarkers["Timestamp", 0] = new MLDouble("", new double[] { (double)evnt.TimeStamp.Ticks }, 1); structEventMarkers["TimestampC", 0] = ConvertDateTimeToMLDouble(evnt.TimeStamp); structEventMarkers["Port", 0] = new MLChar(null, evnt.Port.ToString()); arrayMarkerEvents[k] = structEventMarkers; k++; } structDataStream["MarkerEvents", 0] = arrayMarkerEvents; } // Positions if (gpsDataStream.Positions.Count > 0) { double[][] points = new double[gpsDataStream.Positions.Count][]; int k = 0; foreach (GPSPositionDataLine pt in gpsDataStream.Positions) { points[k] = new double[5]; points[k][0] = Utils.ConvertToUnixTimestamp(pt.TimeStamp); points[k][1] = pt.Lat; points[k][2] = pt.Lon; points[k][3] = pt.Height; points[k][4] = pt.Quality; k++; } MLDouble arrayPoints = new MLDouble("Points", points); structDataStream["Points", 0] = arrayPoints; } } mlList.Add(structDataStream); } try { MatFileWriter mfw = new MatFileWriter(filename, mlList, false); } catch (Exception ex) { logger?.WriteLineError("Error occured: " + ex.ToString()); return; } logger?.WriteLineInfo("MAT file location: " + filename); logger?.WriteLineInfo("Done."); }