Read older MATFILE in the current version.
조회 수: 27 (최근 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
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
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....
There's a link to the description of .mat files V4, V5 available at https://www.mathworks.com/matlabcentral/answers/93868-is-there-documentation-on-the-structure-of-mat-files-in-matlab#answer_103219
채택된 답변
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
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
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
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
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 Center 및 File Exchange에서 Downloads에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!