I am trying to read an older .mat file that I believe is saved as a V.4 style. I am using the latest version of the csmatio library and when I attempt to import it I get a "This is not a valid MATLAB 5.0 MAT-file." error with the std. code and if I comment out that check in the demo program I get a "This version of CSMatIO does not support Big Endian." error. Do you have any thoughts as to how to read the file?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The current version of CSMatIO only supports the MATLAB 5.0 MAT-file. I can put this on backlog of features that we would like to add, but it will not be added anytime soon. Right now, I am in the process or cleaning up the code base and increasing test covereage. After that is done, I will look into adding new features.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
David/Tobias- Thanks for your responses. Unfortunately opening the files up in matlab each time and saving them as a different format is a non-starter for the project I am working on. Do either of you know of any other projects to read Matlab v4 files in either c# or c(++)?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
There is none that I know of. Try looking for a matlab file converter. It has been a long time since I actually used Matlab, but when I did, I remember that it had a decent scripting language. You could write a Matlab script that reads in an old file and writes a new file. You could then have this script called just before importing into your C# program or just after generating the file.
Last edit: David A. Zier 2016-10-19
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The V4 format is much easier than the latter fomats and straight forward to read without a special library. This QND code example might get you started. It produces this output for your file 'DoublePendulum.mat':
:::C#namespaceMatlabV4Reader{usingSystem;usingSystem.Collections.Generic;usingSystem.IO;staticclassProgram{staticvoidMain(string[]args){varbr=newBinaryReader(File.Open("DoublePendulum.mat",FileMode.Open));while(ReadMatrix(br)){}Console.ReadLine();}enumDataType:int{float64=0,float32=10,int32=20,text=51,}privatestaticboolReadMatrix(BinaryReaderbr){if(br.BaseStream.Position==br.BaseStream.Length)returnfalse;inttype=br.ReadInt32();intmrows=br.ReadInt32();intncols=br.ReadInt32();intimagf=br.ReadInt32();intnamlen=br.ReadInt32();stringname=br.ReadCString();vardataType=(DataType)type;List<object>elems=null;switch(dataType){caseDataType.float32:elems=ReadMatrixDataFloat32(br,mrows,ncols);break;caseDataType.int32:elems=ReadMatrixDataInt32(br,mrows,ncols);break;caseDataType.text:elems=ReadMatrixTextData(br,mrows,ncols);break;default:thrownewNotImplementedException(type.ToString());}Console.WriteLine("{1} x {2} {3} matrix named '{0}'",name,mrows,ncols,dataType);//Console.WriteLine("--> element 0: {0}",elems[0]);//Console.WriteLine("--> element 1: {0}",elems[1]);//Console.WriteLine("--> element 2: {0}",elems[2]);//Console.WriteLine("----------------------------------------------------------------");returntrue;}privatestaticList<object>ReadMatrixTextData(BinaryReaderbr,intmrows,intncols){varelems=newList<object>(mrows);for(inti=0;i<mrows;++i)elems.Add(br.ReadNonTerminatedCString(ncols));returnelems;}privatestaticList<object>ReadMatrixDataInt32(BinaryReaderbr,intmrows,intncols){varelems=newList<object>(mrows*ncols);for(inti=0;i<mrows*ncols;++i)elems.Add(br.ReadInt32());returnelems;}privatestaticList<object>ReadMatrixDataFloat32(BinaryReaderbr,intmrows,intncols){varelems=newList<object>(mrows*ncols);for(inti=0;i<mrows*ncols;++i)elems.Add(br.ReadSingle());returnelems;}publicstaticstringReadCString(thisBinaryReaderbr){varchars=newList<char>();charc='\0';do{c=br.ReadChar();chars.Add(c);}while(c!=0);chars.Remove('\0');returnnewstring(chars.ToArray());}publicstaticstringReadNonTerminatedCString(thisBinaryReaderbr,intlen){varchars=br.ReadChars(len);returnnewstring(chars);}}}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I am trying to read an older .mat file that I believe is saved as a V.4 style. I am using the latest version of the csmatio library and when I attempt to import it I get a "This is not a valid MATLAB 5.0 MAT-file." error with the std. code and if I comment out that check in the demo program I get a "This version of CSMatIO does not support Big Endian." error. Do you have any thoughts as to how to read the file?
The current version of CSMatIO only supports the MATLAB 5.0 MAT-file. I can put this on backlog of features that we would like to add, but it will not be added anytime soon. Right now, I am in the process or cleaning up the code base and increasing test covereage. After that is done, I will look into adding new features.
Read the version 4 mat file with Matlab, then save it as version 6 or 7 mat file. Version 6 and 7 should be readable for csmatio.
https://de.mathworks.com/help/matlab/import_export/mat-file-versions.html
David/Tobias- Thanks for your responses. Unfortunately opening the files up in matlab each time and saving them as a different format is a non-starter for the project I am working on. Do either of you know of any other projects to read Matlab v4 files in either c# or c(++)?
There is none that I know of. Try looking for a matlab file converter. It has been a long time since I actually used Matlab, but when I did, I remember that it had a decent scripting language. You could write a Matlab script that reads in an old file and writes a new file. You could then have this script called just before importing into your C# program or just after generating the file.
Last edit: David A. Zier 2016-10-19
The V4 format is much easier than the latter fomats and straight forward to read without a special library. This QND code example might get you started. It produces this output for your file 'DoublePendulum.mat':
Source code:
The spec is here: https://www.mathworks.com/help/pdf_doc/matlab/matfile_format.pdf