MATLAB Answers

Jay
0

How to Delete Arrays?

Asked by Jay
on 29 Jun 2019
Latest activity Commented on by Steven Lord
on 29 Jun 2019
How does one delete arrays (completely) once they have served their specific purpose (saving memory & decluttering the written program)?
I do not want to rename, redimension and then reuse the array (as the coding will inevitably be more difficult to follow and debug).

  1 Comment

Steven Lord
on 29 Jun 2019
If you're writing script files, variables aren't deleted until you explicitly clear them or until the caller's workspace is destroyed. If you're running the scripts from the prompt in the Command Window they're running in the base workspace, which persists until you close MATLAB.
If you're writing function files, the function workspace (and its contents) are destroyed when the function exits after the output arguments have been returned to the caller. [It's a bit more complicated than that with persistent variables, nested functions, etc. but that statement is accurate enough for purposes of this discussion.]
So breaking your program into "bite sized chunks", extracting commonly used steps into functions, can help keep your workspaces organized.

Sign in to comment.

2 Answers

Image Analyst
Answer by Image Analyst
on 29 Jun 2019
 Accepted Answer

If "myArray" is your array name:
clear('myArray');

  2 Comments

Jay
on 29 Jun 2019
Thank you Image Analyst.
I have to get used to arrays being referred to as variables in coding terminology.
Image Analyst
on 29 Jun 2019
Just to expand on this, it could be any variable -- it doesn't have to be an array. You could get rid of tables, cell arrays, structures, class objects, scalars, or whatever.

Sign in to comment.


per isakson
Answer by per isakson
on 29 Jun 2019
Edited by per isakson
on 29 Jun 2019

  0 Comments

Sign in to comment.