clear all variables except specific ones

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

clear all variables except specific ones

dariodematties
Hello People,

How can I clear all variables except a specific subset from them?

There is a function called clearvars in matlab but it is not implemented in my version of Octave.

octave:24> ver
----------------------------------------------------------------------
GNU Octave Version: 4.2.0
GNU Octave License: GNU General Public License
Operating System: Linux 4.4.0-89-generic #112~14.04.1-Ubuntu SMP Tue Aug 1 22:08:32 UTC 2017 x86_64
----------------------------------------------------------------------
Package Name  | Version | Installation directory
--------------+---------+-----------------------
     control  |   3.0.0 | /home/dario/octave/control-3.0.0
          io  |   2.4.5 | /home/dario/octave/io-2.4.5
       ltfat  |   2.1.2 | /home/dario/octave/ltfat-2.1.2
    parallel  |   3.1.1 | /home/dario/octave/parallel-3.1.1
      signal  |   1.3.2 | /home/dario/octave/signal-1.3.2
  statistics  |   1.3.0 | /home/dario/octave/statistics-1.3.0
      struct  |  1.0.14 | /home/dario/octave/struct-1.0.14
octave:25>

octave:25> help clearvars
error: help: the 'clearvars' function is not yet implemented in Octave

Thanks
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: clear all variables except specific ones

Mike Miller-4
On Sat, Aug 12, 2017 at 06:30:29 -0700, dariodematties wrote:
> How can I clear all variables except a specific subset from them?

Probably a couple ways. Here is how you can get a cell array of names of
variables in your workspace:

    v = whos;
    names = {v.name};

or the uglier shortcut

    names = {[whos].name};

From there you can iterate over the list of variable names and clear or
not clear each one.

You could also save the variables you like to a file, clear all, and
load the file back into your workspace.

--
mike

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: clear all variables except specific ones

Martin Weiser-4
On 14.8.2017 20:53, Mike Miller wrote:

> On Sat, Aug 12, 2017 at 06:30:29 -0700, dariodematties wrote:
>> How can I clear all variables except a specific subset from them?
>
> Probably a couple ways. Here is how you can get a cell array of names of
> variables in your workspace:
>
>     v = whos;
>     names = {v.name};
>
> or the uglier shortcut
>
>     names = {[whos].name};
>
> From there you can iterate over the list of variable names and clear or
> not clear each one.
>
> You could also save the variables you like to a file, clear all, and
> load the file back into your workspace.
>
>
>
> _______________________________________________
> Help-octave mailing list
> [hidden email]
> https://lists.gnu.org/mailman/listinfo/help-octave
>
Hi,

Would not be something like:

clear -x the_one_i_need

or

clear -x *[0-9] #clear those that do not end by digit

more elegant?

Of course, the usability of the above stands and fails with the length
of the list you wish to retain or upon your ability to find the matching
pattern.

See:
https://www.gnu.org/software/octave/doc/interpreter/Status-of-Variables.html#index-clear

Best,
Martin W.

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: clear all variables except specific ones

dariodematties
This post was updated on .
Great Martin!

It seems to work perfectly.

Thanks a lot.

Thank you Mike too. Your solution could be useful for a case in which I would have to keep an amount of variables that is too big to be listed by hand.

Thank you people,

Dario
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: clear all variables except specific ones

Mike Miller-4
In reply to this post by Martin Weiser-4
On Tue, Aug 15, 2017 at 01:17:41 +0200, Martin Weiser wrote:
> Would not be something like:
>
> clear -x the_one_i_need
>
> or
>
> clear -x *[0-9] #clear those that do not end by digit
>
> more elegant?

Good catch, that looks equivalent to clearvars -except.

--
mike

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave

signature.asc (849 bytes) Download Attachment
Loading...