Memory during a loop

7 Ansichten (letzte 30 Tage)
Aadil
Aadil am 6 Sep. 2012
Hi,
If I run Clear All during a loop, will it also clear the loop value?
Thanks,
  1 Kommentar
Aadil
Aadil am 6 Sep. 2012
ok it seems it does, is there any way round this, I'm reading large .mat data files, any way to only clear them and not the previous variables?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 6 Sep. 2012
As usual I mention, that clear all removes all loaded functions from the memory and reloading them from the disk and parsing them wastes a lot of time. When you want to clear variables, use
clear variables
However, Arthur's hint to use S=load() is very good. 1. it is easy to clear, 2. you cannot overwrite local variables by accident (as e.g. the loop counter...), 3. The debugging is much easier, because you can see on first sight, that "S.abcde" comes from the loaded file, and 4. Matlab is much faster with fixed names of variables compared to the ones created dynamically by EVAL or LOAD without catching the output.
  1 Kommentar
Aadil
Aadil am 7 Sep. 2012
hmm ok
so if I have:
for WhichVehicle = 1 : length(ProcessedFolderDirectories)
load(blahblah.mat)
run(blahblah)
clear variables
end
So it will only clear the variables then?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Arthur
Arthur am 6 Sep. 2012
sure, you can just clear the variable:
clear varname
check:
doc clear
  3 Kommentare
Arthur
Arthur am 6 Sep. 2012
Do you need to load all the variables at once? You can also load a single variable at the time.
S = load(filename, variables)
This can be more memory efficient, but you may have to open the file multiple times...
Jan
Jan am 6 Sep. 2012
+1. Especially the suggested "S=load()" is a very good idea.

Melden Sie sich an, um zu kommentieren.


Sean de Wolski
Sean de Wolski am 6 Sep. 2012
Bearbeitet: Sean de Wolski am 6 Sep. 2012
It depends on where you call it, compare:
  • 1
for ii = 1:10
ii
clear all
end
  • 2
for ii = 1:10
clear all
ii
end
But of course Arthur is right - you shouldn't need to use clear all at all.
  2 Kommentare
Aadil
Aadil am 6 Sep. 2012
is there any way to clear the last mat file loaded?
Sean de Wolski
Sean de Wolski am 6 Sep. 2012
Assign the *.MAT file you load to a variable then clear it when you're done. That way you aren't poofing variables into your workspace.
for ii = stuff
S = load('mymatfile.mat');
do_stuff_with(S)
clear S
end

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by