User1551892
User1551892

Reputation: 3364

How to clear the content or values of variable in MATLAB

I would like to know whether there is a way to keep the name of variables but erase the content of these variables. I do not want to reiterate the definition of these variables. I would like to erase the content of variables to avoid mixing the data from different runs.

Upvotes: 0

Views: 1150

Answers (1)

matlabgui
matlabgui

Reputation: 5672

Its messy and I wouldn't do it, but I "think" this is what your after:

function resetBaseWorkspace 
  vars = evalin ( 'base', 'whos' );
  for ii=1:length(vars)
    evalin ( 'base', sprintf ( '%s = []', vars(ii).name ) );
  end
end

Run the above function when you want to clear the vars in the base workspace.

To reset structs - you could use the above theory (but its very messy code...)...

So why dont you save a mat file containing your structs with "empty" fields and load it at the start of your process?

Or use functions instead?

Upvotes: 1

Related Questions