Filter löschen
Filter löschen

Clearing workspace variables while in a loop.

16 Ansichten (letzte 30 Tage)
Kyle Reagan
Kyle Reagan am 4 Dez. 2016
Kommentiert: Walter Roberson am 5 Dez. 2016
I have a while loop that contains a menu and some options for the user to input. One of them is to delete the workspace variables. However, whenever I clear the workspace it doesn't actually clear until the while loop stops (this occurs when they exit the program).
% case 3
% Clear data from memory
clearvars -except continueprogram %
% This clears the entire workspace, while allowing the
% program to continue running.
%
clc;
fprintf('The memory is cleared. Press a key to continue. \n')
pause;
  3 Kommentare
Kyle Reagan
Kyle Reagan am 4 Dez. 2016
The loop is a data analysis tool for an uploaded file. It does things like plot histograms, linear regression, etc. One of the options on my menu is "Clear All" so that the user can clear all the file data, and then upload a new file without ever having to exit from the program.
Stephen23
Stephen23 am 5 Dez. 2016
Clearing variables in a loop seems like an inefficient way to write code. Either the variables are re-allocated in each iteration (in which case clearing them serves no purpose) or new variables are created on each iteration (in which case the code is going to be buggy and slow no matter what).
Clearing variables is slow. Code of this complexity should be written as a function (and not a script), in which case it is totally irrelevant to the end user what variables exist in the function workspace.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Walter Roberson
Walter Roberson am 4 Dez. 2016
How can you tell that the workspace is not actually cleared?
If you are using the Workspace Browser then you need to know that (at least in recent versions) the Workspace Browser is not updated until you stop in the debugger or use keyboard() or control returns to the command line.
  6 Kommentare
Kyle Reagan
Kyle Reagan am 4 Dez. 2016
This clears the "continueprogram = true" variable and causes the loop to stop running.
Walter Roberson
Walter Roberson am 5 Dez. 2016
evalin('base', 'clearvars -except continueprogram')

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Debugging and Analysis 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