从.mat文件中删除变量 [英] deleting variables from a .mat file

查看:820
本文介绍了从.mat文件中删除变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有人知道如何从matlab文件中删除变量吗?我知道您可以使用save -append方法将变量添加到现有的matlab文件中,但是没有有关如何从文件中删除变量的文档.

Does anyone here know how to delete a variable from a matlab file? I know that you can add variables to an existing matlab file using the save -append method, but there's no documentation on how to delete variables from the file.

在有人说只保存它"之前,这是因为我将中间处理步骤保存到磁盘上以减轻内存问题,最后每个分析例程将有近10 GB的中间数据.谢谢!

Before someone says, "just save it", its because I'm saving intermediate processing steps to disk to alleviate memory problems, and in the end there will be almost 10 GB of intermediate data per analysis routine. Thanks!

推荐答案

足够有趣的是,您可以将-append选项与

Interestingly enough, you can use the -append option with SAVE to effectively erase data from a .mat file. Note this excerpt from the documentation (bold added by me):

对于MAT文件,-append将新变量添加到文件中,或者将现有变量的已保存值替换为工作区中的值.

For MAT-files, -append adds new variables to the file or replaces the saved values of existing variables with values in the workspace.

换句话说,如果.mat文件中的变量称为A,则可以使用>的 new 副本保存该变量(您已将其设置为[])使用-append选项. .mat文件中仍然存在一个名为A的变量,但该变量将为空,从而减小了文件的总大小.

In other words, if a variable in your .mat file is called A, you can save over that variable with a new copy of A (that you've set to []) using the -append option. There will still be a variable called A in the .mat file, but it will be empty and thus reduce the total file size.

这是一个例子:

>> A = rand(1000);            %# Create a 1000-by-1000 matrix of random values
>> save('savetest.mat','A');  %# Save A to a file
>> whos -file savetest.mat    %# Look at the .mat file contents
  Name         Size                Bytes  Class     Attributes

  A         1000x1000            8000000  double

文件大小约为7.21 MB.现在执行此操作:

The file size will be about 7.21 MB. Now do this:

>> A = [];                              %# Set the variable A to empty
>> save('savetest.mat','A','-append');  %# Overwrite A in the file
>> whos -file savetest.mat              %# Look at the .mat file contents
  Name      Size            Bytes  Class     Attributes

  A         0x0                 0  double

现在文件大小约为169个字节.该变量仍在其中,但为空.

And now the file size will be around 169 bytes. The variable is still in there, but it is empty.

这篇关于从.mat文件中删除变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆