Difference between clear var and clear var*

1 Ansicht (letzte 30 Tage)
Rowan Lawrence
Rowan Lawrence am 30 Aug. 2019
Kommentiert: Rowan Lawrence am 31 Aug. 2019
Hi all,
This will probably be a very straightforward question but I can't get to the bottom of it. I just started reading Mike Cohen's book 'MATLAB for Brain and Cognitive Scientists' this afternoon. He suggests checking the difference between 'clear a' and clear a*' in one of the preliminary chapters. They don't appear to do anything different. I have read the MATLAB documentation for 'clear' again (not to much to chew on), and I can't see anything.
Given what little I know about pointers from C++, I initially imagined that clear a* would remove the value stored in a from the workspace, but that the memory allocated to it would remain (just empty); and that clear a would remove variable a from the workspace entirely. I then checked both with 'whos' and both commands simply remove the variable and its value.
I downloaded a set of tip scripts from Mike's website for the book. The tip for that part of the chapter is simply:
%% 6
clear a
clear a*
In a last-ditch effort, I Googled it in a few different ways, but turned up nothing.
There are a thousand more interesting issues and questions I could ask, but this is just driving me up the wall because I can't find the answer, even though I know it will be simple. Does anybody know what exactly he is trying to get at with this?
Cheers!
Rowan.

Akzeptierte Antwort

Steven Lord
Steven Lord am 31 Aug. 2019
Let's start with a blank workspace.
>> clear all
Create three variables and check that they all exist in the workspace.
>> a = 1;
>> ab = 2;
>> abc = 3;
>> whos
Name Size Bytes Class Attributes
a 1x1 8 double
ab 1x1 8 double
abc 1x1 8 double
Run the first command. What's in the workspace now?
>> clear a
>> whos
Name Size Bytes Class Attributes
ab 1x1 8 double
abc 1x1 8 double
Run the second command. What's in the workspace now?
>> clear a*
>> whos
>>
If you're not certain exactly what a particular command does, I recommend checking its help text and/or its documentation. In this case, the help text describes how "clear a*" is interpreted.
>> help clear
  1 Kommentar
Rowan Lawrence
Rowan Lawrence am 31 Aug. 2019
Ah, so the * is acting as a wildcard for the variable names. I had assumed it was related to the memory of the variable, which is why I was confused (probably why I couldn't find anything in the docs!)
Thankyou for the answer Steven; sanity returned.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Startup and Shutdown 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