Clearing workspace variables while in a loop.

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

Why would you want to clear variables while a loop is running? This does not seem like a wise thing to do. Please provide justification for doing this.
Kyle Reagan
Kyle Reagan 2016년 12월 4일
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 2016년 12월 5일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2016년 12월 4일

0 개 추천

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

Kyle Reagan
Kyle Reagan 2016년 12월 4일
I created a variable "a = 5" in the command window. Then I ran my loop which functions through a menu with various options. It has a pause feature after each action. So after I pressed "Clear All" on the menu ("clearvars -except continueprogram" is how it works), I did "5*a" in the command window and got 25.
"clearvars removes all variables from the currently active workspace."
You are clearing the variables from the workspace of the callback function, which is not touching the variables of the base workspace.
Kyle Reagan
Kyle Reagan 2016년 12월 4일
Whenever I use any type of clear the same thing happens. Do you know of a way to clear the base workspace while the loop continues?
evalin('base', 'clear')
Kyle Reagan
Kyle Reagan 2016년 12월 4일
This clears the "continueprogram = true" variable and causes the loop to stop running.
evalin('base', 'clearvars -except continueprogram')

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

질문:

2016년 12월 4일

댓글:

2016년 12월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by