Read older MATFILE in the current version.

22 views (last 30 days)
Hi, I have very old matlab files. Current Matlab dose not support the old format. I have tried reading it in Python and R. Python does read it but the output data is not correct. Is there a way to read these files and retreive the data. I have attached a test file for the reference. Any help would be great!
  7 Comments
Walter Roberson
Walter Roberson on 26 Apr 2023
https://www.mathworks.com/matlabcentral/fileexchange/22675-read-vaxd-and-vaxg-files-in-r2008b-and-later
https://www.mathworks.com/matlabcentral/answers/13901-converting-vax-float-to-matlab-float
dpb
dpb on 26 Apr 2023
Table 1-8, Level 4 MAT-File Matrix Header Format
Field Description
type The type flag contains an integer whose decimal digits encode storage information. If the
integer is represented as MOPT where M is the thousands digit, O is the hundreds digit, P is the
tens digit, and T is the ones digit, then:
M indicates the numeric format of binary numbers on the machine that wrote the file. Use this
table to determine the number to use for your machine:
0 IEEE Little Endian (PC, 386, 486, DEC Risc)
1 IEEE Big Endian (Macintosh, SPARC®, Apollo, SGI, HP
9000/300, other Motorola® systems)
2 VAX D-float
3 VAX G-float
4 Cray
...
Looking at the given file (had to do this locally, couldn't seem to fopen the attachment)...
>> fn='test_read.mat';
>> fid=fopen(fn,'r');
>> type=fread(fid,1,'int4')
>> type =
3000
>> fid=fclose(fid);
That would indeed, seem to be a MATLAB 4 VAX G-float array....

Sign in to comment.

Accepted Answer

DGM
DGM on 26 Apr 2023
Edited: DGM on 26 Apr 2023
FWIW, I was able to use Walter's first link to read the file.
myfilename = 'test_read.mat';
fid = fopen(myfilename, 'r', 'ieee-le');
% for sake of simplicity, i'm going to just presume M0PT is always 3000
% always VAX-G, double-precision, numeric
fseek(fid,4,-1);
% get the matrix size
nrows = fread(fid, 1, 'uint32');
ncols = fread(fid, 1, 'uint32');
% i'm going to just ignore the possibility that the matrix is complex-valued
fseek(fid,4,0);
% read the original variable name
namelen = fread(fid, 1, 'uint32');
varname = fread(fid, namelen-1, '*char')
fseek(fid,1,0); % name is followed by a null character
% read the matrix
A = freadVAXG(fid, [nrows ncols], 'double');
% i assume there's only one header in the stream, so just close the file
fclose(fid);
% reshape the data and test it against Steven's recovered version
A = reshape(A,nrows,[]);
load new_test_read.mat
immse(A,E) % they match
Of course, I'm assuming that other files are similar. (real-valued numeric double in VAX-G format, with only a single matrix in the file)
The details of the MAT-file format are specified here:
See page 1-25 for the V4 format
  9 Comments
dipak sanap
dipak sanap on 27 Apr 2023
@DGM, it worked, Idk, I just restarted the Matlab and moved everything to clear directory. Thank you so much! I just have a final question. How can I make it work for files with different size arrays? Like just row or vector or matrix?
DGM
DGM on 27 Apr 2023
As I understand it, the V4 format only supports 2D arrays, and the size of the arrays should be fetched from the header (nrows x ncols). Unless I'm missing something, the given example should already support variations in array sizes (so long as the other assumptions don't change).

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 26 Apr 2023
I tried loading the original data using the oldest release of MATLAB I can run and was able to read it. It must have been created by a very old release (pre-v4 perhaps?). I resaved it as a newer MAT-file, attached here.
  6 Comments
Cris LaPierre
Cris LaPierre on 27 Apr 2023
I found I could open the file in R2007b, and R2008a, but not R2008b.
I couldn't launch anything earlier than that on my computer.
Steven Lord
Steven Lord on 27 Apr 2023
Actually now that I think about it I didn't use the oldest release I believe can run on my machine (which is different from the oldest release whose files I could access.) I went as far back as release R11 (even though that's not remotely close to a supported release on my OS) because I know I can run that release with only a little difficulty.
Since I work on functions that have existed in some cases since Cleve's original MATLAB (predating MathWorks!) sometimes it's useful to perform "archaeology" to figure out when a particular behavior was introduced and that can entail reading or running code from very old releases.
Is there a TMW service to translate old files that have been orphaned by dropping support for early releases, Steven? That would seem to be a valuable service that a 'bot could handle like the "convert pdf to Excel" services that abound.
I don't know if there is such a service, though it's an interesting idea.

Sign in to comment.

Categories

Find more on Downloads in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!