C.B.
C.B.

Reputation: 80

How to remove global flag from variable in workspace

I have some data (.mat) which was set to global as it was saved and which is now saved as a global cell. When I load it into the workspace it automatically is set to global.

Is there a way to remove the global flag from this variable, without removing the variable itself from the workspace, only the global attribute?

When I copy this array it automatically copies its global attribute as well and in the documentation it only says how to set to global and not how to remove it. I am using MATLAB R2015a.

global exportmat
exportmat = cell(889,12);
filename = 'test.mat';
save(filename)

clear -globals exportmat

load('test.mat')
whos

  Name              Size            Bytes  Class    Attributes

  exportmat       889x12            85344  cell     global   

Upvotes: 3

Views: 148

Answers (1)

Adriaan
Adriaan

Reputation: 18177

The easiest method, RAM permitting, I could find was to simply redeclare it:

global A
A=3;
whos A
  Name      Size            Bytes  Class     Attributes

  A         1x1                 8  double    global    
B=A;
whos B
  Name      Size            Bytes  Class     Attributes

  B         1x1                 8  double               % Note: not global
clear -global
A=B;
clear B;
whos A
  Name      Size            Bytes  Class     Attributes

  A         1x1                 8  double         

If you need this variable more often, simply use this to remove the global flag and save it again.

Using save and load on R2016b:

global exportmat
exportmat = cell(889,12);
filename = 'test.mat';
save(filename)

clear exportmat
load('test.mat')
% whos exportmat

exportmat2=exportmat;

whos

  Name              Size            Bytes  Class    Attributes

  exportmat       889x12            85344  cell     global    
  exportmat2      889x12            85344  cell               
  filename          1x8                16  char               

If R2015a doesn't work for cells (I can't check that since I don't have that version), you can reassign each cell contents, which should work if they contain doubles:

B = cell(size(A));
for ii = 1:size(B,1)
    for jj = 1:size(B,2)
        tmp = A{ii,jj};
        B{ii,jj} = tmp;
    end
end

Upvotes: 3

Related Questions