1

I have in my file .mat three matrices, I need to delete one of them.

I try clear ('Matrice1'), but it doesn't work.

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
Guillaume
  • 123
  • 4
  • 13
  • 2
    Possible duplicate of [deleting variables from a .mat file](http://stackoverflow.com/questions/4268044/deleting-variables-from-a-mat-file) – Sardar Usama Aug 25 '16 at 15:54
  • It isn't a duplicate question, before in that link the problem is that the programer is not able to save all its results after a new execution, so the solution was to add '-append' but in my case i need just to delete a variable from a .mat file which is already existed. – Guillaume Aug 25 '16 at 16:02
  • rewriting a variable with `[]` deletes that variable. They have used append to rewrite the variable with `[]`. – NKN Aug 25 '16 at 16:26
  • @NKN not exactly.. The variable still exists in the file, but its _contents_ are now an empty array. – Dev-iL Aug 25 '16 at 16:28
  • @Dev-iL correct, but is your answer giving a way for deleting the variable? – NKN Aug 25 '16 at 16:32
  • 1
    @NKN Nope. This is why I said "if acceptable". BTW, another related question: http://stackoverflow.com/q/35323694/3372061. There's also [this FEX submission](http://www.mathworks.com/matlabcentral/fileexchange/162-delvarmat) – Dev-iL Aug 25 '16 at 16:36

2 Answers2

1

The closest thing to deleting a variable I can think of is replacing it with an empty array. If this is acceptable, you can either do it with the methods in the question Sardar_Usama mentioned, or using matfile like so:

% Let's say the mat-file is called "matlab.mat"
a = matfile('H:\PathToFile\matlab.mat','Writable',true)

Output:

a = 

  matlab.io.MatFile

  Properties:
      Properties.Source: 'H:\PathToFile\matlab.mat'
    Properties.Writable: true                                          
                 SIZE_X: [1x1 double]                                  
                 SIZE_Y: [1x1 double]  

Then you can do:

a.SIZE_X = []

And get:

a = 

  matlab.io.MatFile

  Properties:
      Properties.Source: 'H:\PathToFile\matlab.mat'
    Properties.Writable: true                                          
                 SIZE_X: [0x0 double]                                  
                 SIZE_Y: [1x1 double] 

After doing this, no further actions are required. The variable inside the file will have the new value ([] in this case).


P.S.

I'm providing this answer because the linked question is from ~6 years ago, when the matfile option didn't exist yet (added in R2011b).

Community
  • 1
  • 1
Dev-iL
  • 23,742
  • 7
  • 57
  • 99
1

If you absolutely must completely delete the entire variable the most straightforward option is to load the data, remove the variable, and then resave it. Because we have to load and save again, this method is very likely far less efficient than using memmapfile or using save to change the stored variable to an empty array.

For example:

function testcode
% Generate sample data
a = rand(12);
b = rand(12);
c = rand(12);
save('test.mat');

% Remove and test
rmmatvar('test.mat', 'c');
whos('-file', 'test.mat');
end

function rmmatvar(matfile, varname)
% Load in data as a structure, where every field corresponds to a variable
% Then remove the field corresponding to the variable
tmp = rmfield(load(matfile), varname);
% Resave, '-struct' flag tells MATLAB to store the fields as distinct variables
save(matfile, '-struct', 'tmp');
end

Which gives the following output:

  Name       Size            Bytes  Class     Attributes

  a         12x12             1152  double              
  b         12x12             1152  double      
sco1
  • 12,154
  • 5
  • 26
  • 48
  • isn't `rmvar` a function from Fuzzy Toolbox and `rmfield` from mapping toolbox? (+1) – NKN Aug 25 '16 at 16:42