Read older MATFILE in the current version.

23 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2023-4-26
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 2023-4-26
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....

请先登录,再进行评论。

采纳的回答

DGM
DGM 2023-4-26
编辑:DGM 2023-4-26
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 个评论
dipak sanap
dipak sanap 2023-4-27
@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 2023-4-27
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).

请先登录,再进行评论。

更多回答(1 个)

Steven Lord
Steven Lord 2023-4-26
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 个评论
Cris LaPierre
Cris LaPierre 2023-4-27
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 2023-4-27
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Downloads 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by