Trying to preserve the internal old mat file date

1 次查看(过去 30 天)
Hi Guys, I want to modify some mat file parameters inside matlab and then save the new mat with the same name (overwrite the old file with new parameters) But I want also to preserve the internal date of the mat file. For example: In linux when I want to reset date of file I will just do:
touch -d "new date" file_name
But mat files have Internal date: For example:
"MATLAB 5.0 MAT-file, Platform: GLNXA64, Created on: Mon Aug 3 19:40:28 2015" The first line of the mat file opened in NotePad++ for example.
How can I change mat file and preserve this date? Thanks,

采纳的回答

Guillaume
Guillaume 2016-7-13
编辑:Guillaume 2016-7-13
Considering that there's no function in matlab that can read this creation date, why do you care?
There is also no function that allows to change it. If it's really that important to you, you could possibly hack it by reading the file as binary before and after modification and replacing the date portion of the header. Note that reading the file as text would be too dangerous, code page conversions may corrupt the binary portion of the file.
Something like the following may work. It may also irreversibly corrupt the file:
fid = fopen('yourfile.mat', 'r'); %open as binary, read-only
oldcontent = fread(fid); %read the whole lot as bytes;
fclose(fid);
%...
%do your modifications
fid = fopen('modifiedfile.mat', 'r+'); %open as binary, for reading and writing
newcontent = fread(fid);
%It would appear that the text portion of the header ends at byte 116.
%I haven't looked if this is guaranteed by the format
%If it is not, some files will get corrupted
assert(oldcontent(117) == 0, 'Unexpected byte in old file');
assert(newcontent(117) == 0, 'Unexpected byte in new file');
oldstring = char(oldcontent(1:116)');
newstring = char(newcontent(1:116)');
olddate = regexp(oldstring, '(?<=Created on: ).{3} .{3} \d+ \d+:\d+:\d+ \d{4} ', 'match', 'once');
assert(~isempty(olddate), 'Could not find date in old file');
%Note that there may be some text after the creation date that would need shifting
extents = regexp(newstring, '(?<=Created on: )(.{3} .{3} \d+ \d+:\d+:\d+ \d{4} )(.*?)( +$)', 'tokenExtents', 'once');
assert(extents(1, 1) + numel(olddate) + diff(te(2, :)) + 1 <= 116, 'Not enough room to fit date')
newstring(extents(1, 1) : extents(1, 1) + numel(olddate) + diff(extents(2, :))) = [olddate, newstring(extents(2, 1):extents(2, 2))];
frewind(fid);
fwrite(fid, newstring);
fclose(fid);
Note that I have files with this header: MATLAB 7.3 MAT-file, Platform: PCWIN, Created on: Tue Feb 09 20:24:01 2016 HDF5 schema 1.00 ., so the code above makes sure that the text after the date is shifted appropriately.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by