Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does MATLAB keep some variables after clearing?

Tags:

memory

matlab

I have a program that reads in a really large Excel file and creates some large variables. This runs out of storage if I try to run it multiple times in a row, which makes sense, i.e.:

large_program; large_program

will crash. However, what I don't understand is why

large_program; clear all; large_program

will also crash; in order to run it multiple times, I have to restart MATLAB each time. Does MATLAB not actually clear all variables? Or is this a fragmentation of memory thing?

like image 479
Tim Hazard Avatar asked Jul 13 '12 20:07

Tim Hazard


People also ask

Do workspace variables persist after you exit MATLAB?

Workspace variables do not persist after you exit MATLAB. To use your data across multiple sessions, save it to a compressed file with a . mat extension called a MAT-file. You can restore saved data by loading a MAT-file back into MATLAB.

What is the difference between clear and clear all in MATLAB?

clear FUNCTIONS removes all compiled MATLAB and MEX-functions. clear ALL removes all variables, globals, functions and MEX links.

Should I use clear all in MATLAB?

Using CLEAR ALL usually decreases code performance and is often unnecessary - MATLAB Answers - MATLAB Central.

What is persistent variable MATLAB?

A persistent variable is a local variable in a MATLAB® function that retains its value in memory between calls to the function. If you generate code from your model, you must initialize a persistent variable for your MATLAB functions. For more information, see persistent .


1 Answers

Matlab can indeed hold onto some variables and other settings "under the hood". I have the following set up as a short-cut to purge it back to a "just switched on" state (the one that can really catch you out is that clear functions is not a subset of clear all).

restoredefaultpath;
clc;
clear all;
close all;
clear functions;
bdclose('all');
fclose('all');
like image 157
BigA Avatar answered Sep 30 '22 00:09

BigA