bugGNU Octave - Bugs: bug #40095, Clearvar function not implemented

 
 

bug #40095: Clearvar function not implemented

Submitter:  None
Submitted:  Tue 24 Sep 2013 02:02:42 PM UTC
   
 
Category:  Octave Function Severity:  1 - Wish
Priority:  3 - Low Item Group:  Matlab Compatibility
Status:  Duplicate Assigned to:  None
Originator Name:  Federico Originator Email:  -email is unavailable-
Open/Closed:  * Closed Release:  * dev
Operating System:  * Any Fixed Release:  None
Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Tue 20 Mar 2018 10:13:30 AM UTC, comment #8: 

Last update I promise! Tidied up and added another test case (keepVariables).


(file #43599, file #43600)

Ceral Paquet <octavebugs>
Fri 16 Mar 2018 07:06:04 PM UTC, comment #7: 

OK I extended the test suite, fixed a couple of bugs and added the Octave copyright notice. Files are attached.

BTW I've checked the consistency with MATLAB behaviour which also passes (except the global variable test at the end which doesn't like definition/assignment on the same line and wants the function definition at the end of the file. However when I correct those incompatibilities it also passes).

So, this clearvars behaves just like MATLAB.

(file #43577, file #43578)

Ceral Paquet <octavebugs>
Fri 16 Mar 2018 12:45:34 PM UTC, comment #6: 

I'd like to contribute this working implementation of clearvars.m based heavily on what David Turner did earlier. It has one minor bug relating to Octave's handling of ans in evalin (https://savannah.gnu.org/bugs/?53359).


function clearvars(varargin)

  count = 0;
  global_mode = false;
  regexp_mode = false;
  except_mode = false;

  # parse argument list
  for arg = 1:nargin

    curr_arg = varargin{arg};

    if strcmp(curr_arg, "-global")
      global_mode = true;
      continue;
    endif

    if strcmp(curr_arg, "-except")
      except_mode = true;
      continue;
    endif

    if strcmp(curr_arg, "-regexp")
      regexp_mode = true;
      continue;
    endif

    count += 1;
    vars(count).except = except_mode;
    vars(count).global = global_mode;
    vars(count).regexp = regexp_mode;
    vars(count).var_name = curr_arg;

  endfor

  # do we need a wildcard?
  if count == 0 || all([vars.except])
    count += 1;
    vars(count).except = false;
    vars(count).global = false;
    vars(count).regexp = false;
    vars(count).var_name = '*';
  endif

  # expand regular expressions
  for c = 1:count

    eval_str = 'who(';

    if vars(c).global
        eval_str = [eval_str '"global",'];
    endif

    if vars(c).regexp
      if isempty(strcmp(vars(c).var_name,'*'))
        vars(c).var_name = ['*' vars(c).var_name '*'];
      endif
    endif

    eval_str = [eval_str '"' vars(c).var_name '")'];

    # replace with cell array of matching strings
    vars(c).var_name = evalin('caller',eval_str);

  endfor

  # clear variables one by one
  for c1 = find([vars.except]==false)
    for v1 = 1:numel(vars(c1).var_name)

      # handle excepts (brute force)
      do_clear = true;

      for c2 = find([vars.except]==true)
        for v2 = 1:numel(vars(c2).var_name)

            do_clear &= ~isequal(vars(c1).var_name{v1},vars(c2).var_name{v2});

        endfor
      endfor

      if do_clear == true

        eval_str = 'clear ';

        if vars(c).global
            eval_str = [eval_str ' -global '];
        endif

        eval_str = [eval_str ' ' vars(c1).var_name{v1}];

        evalin("caller",eval_str);

      endif

    endfor
  endfor

endfunction


Ceral Paquet <octavebugs>
Mon 11 Sep 2017 02:57:35 PM UTC, comment #5: 

Updated local Windows Octave version to 4.2.1
I tried the patch saving it under C:\Octave\Octave-4.2.1\share\octave\4.2.1\m\general path and launched a script that uses clearvar function.

result is the following:
####
error: Invalid call to strjoin.  Correct usage is:

 -- STR = strjoin (CSTR)
 -- STR = strjoin (CSTR, DELIMITER)
error: called from
    print_usage at line 91 column 5
    strjoin at line 56 column 5
    clearvars at line 68 column 3
    xxxxxxxxx at line 22 column 5
####

Is the patch out of date?

Federico Gennari <fedemone>
Tue 22 Nov 2016 10:03:44 PM UTC, comment #4: 

The "clearvars" function is still missing in Octave 4.2.0.

This new function is submitted as a patch in comment #3.

Hartmut <hardy>
Fri 18 Dec 2015 10:37:55 AM UTC, comment #3: 

Federico, the .m file in the changeset relies on the other changes to the C++ code.

The attached .m file runs with the current codebase.

(file #35789)

Lachlan Andrew <lachlan>
Thu 20 Mar 2014 03:19:50 PM UTC, comment #2: 

I tried with a pre-release version (octave-3.8.1-1) of Windows build found here:
http://mxeoctave.osuv.de/

and clearvars is still not present. Also, trying to use as .m file the patch kindly provided here is not working.

Why the fix has not been ported? Should we expect it in official Windows build?

Federico Gennari <fedemone>
Mon 30 Sep 2013 02:44:16 AM UTC, comment #1: 

I've attached a patch for this (I also decided to patch the who function, because it didn't match what matlab did).

David M. Turner <novalis>
Tue 24 Sep 2013 02:02:42 PM UTC, original submission:  

The function Clearvar existing in Matlab (and described here:
http://www.mathworks.it/it/help/matlab/ref/clearvars.html )is not implemented in Octave and not recognized, giving corectly an error message.

Such function is not present in Source Forge listing: http://octave.sourceforge.net/functions_by_alpha.php

Could it be implemented?

Anonymous

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

Attached Files
file #43599:  clearvars.m added by octavebugs (3KiB - text/x-objcsrc)
file #43600:  test_clearvars.m added by octavebugs (2KiB - text/x-objcsrc)
file #43580:  clearvars.m added by octavebugs (3KiB - text/x-objcsrc - Tidied up slightly. Use this version!)
file #43577:  clearvars.m added by octavebugs (3KiB - text/x-objcsrc)
file #43578:  test_clearvars.m added by octavebugs (1KiB - text/x-objcsrc)
file #35789:  clearvars.m added by lachlan (4KiB - text/x-objcsrc)
file #29264:  fix-40095 added by novalis (6KiB - application/octet-stream - Patch)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by rik5 (Updated the item)
  • -email is unavailable- added by octavebugs (Posted a comment)
  • -email is unavailable- added by hardy (Posted a comment)
  • -email is unavailable- added by mtmiller (Updated the item)
  • -email is unavailable- added by lachlan (Updated the item)
  • -email is unavailable- added by fedemone (Posted a comment)
  • -email is unavailable- added by jordigh (Updated the item)
  • -email is unavailable- added by novalis (Updated the item)
  • -email is unavailable- added by None (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 14 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2018-12-05 rik5 StatusPatch Submitted Duplicate
        Open/ClosedOpen Closed
    2018-03-20 octavebugs Attached File- Added clearvars.m, #43599
        Attached File- Added test_clearvars.m, #43600
    2018-03-16 octavebugs Attached File- Added clearvars.m, #43580
    2018-03-16 octavebugs Attached File- Added clearvars.m, #43577
        Attached File- Added test_clearvars.m, #43578
    2016-03-31 mtmiller Severity3 - Normal 1 - Wish
        Priority5 - Normal 3 - Low
        Release3.6.4 dev
        Operating SystemMicrosoft Windows Any
    2015-12-18 lachlan Attached File- Added clearvars.m, #35789
    2013-09-30 jordigh StatusNone Patch Submitted
    2013-09-30 novalis Attached File- Added fix-40095, #29264

    Back to the top

    Powered by Savane 3.13-3230.
    Corresponding source code