xiaoxin960904的个人博客分享 http://blog.sciencenet.cn/u/xiaoxin960904

博文

[转载]Matlab Recover data from a corrupt MAT-file

已有 1824 次阅读 2021-3-31 09:42 |个人分类:Matlab学习笔记|系统分类:科研笔记|文章来源:转载

来源:https://www.mathworks.com/matlabcentral/answers/98890-how-do-i-recover-data-from-a-corrupt-mat-file

有时候MATLAB读取mat文件会 File might be corrupt 的错误

数据其实是保存到mat里了,只是部分变量在保存过程中出现了问题(a corrupt zero-byte tag

底下的 MathWorks Support Team 给出了可靠的解决办法,下面附程序

其实通过这个程序我们可以窥探出mat文件是如何储存变量的

因此可以用来只读取某些变量的某些部分

主程序:

splitmat corruptMATFileName % Do not include the .mat extension

load('<corrupted_file>.mat', '<uncorrupted_variable>');

函数

function splitmat(filename)

f=fopen([filename '.mat'], 'rb');

header=fread(f,128);

i=1;

while true

    h2=fread(f,2,'int32');

    if length(h2) < 2

        disp 'finished reading file';

        break;

    end

    if h2(2) == 0

        disp(sprintf('Found bad 0-byte size at variable #%d.', i));

        break;

    end

    fout=fopen(sprintf('%s_%d.mat', filename, i), 'wb');

    fwrite(fout, header);

    fwrite(fout, h2, 'int32');

    data = fread(f, h2(2));

    fwrite(fout, data);

    fclose(fout);

    i = i+1;

end

fclose(f)




https://wap.sciencenet.cn/blog-3386114-1279443.html

上一篇:Matlab 关于NaN值的填充
下一篇:Matlab 粗箭头绘制
收藏 IP: 222.195.137.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-18 19:36

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部