bugGNU Octave - Bugs: bug #60157, save() expressive filtering when...

 
 

bug #60157: save() expressive filtering when using brackets [ ] does not appear to work.

Submitted by:  N Kando <itskando>
Submitted on:  Tue 02 Mar 2021 08:16:49 PM UTC  
 
Category:  Documentation Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Unexpected Error or Warning
Status:  Fixed Assigned to:  None
Originator Name:  itskando Open/Closed:  Closed
Release:  6.1.0 Operating System:  Any

Add a New Comment (Rich Markup)
   

You are not logged in

Please log in, so followups can be emailed to you.

 

( Jump to the original submission)

Mon 08 Mar 2021 07:17:16 PM UTC, comment #12: 

comment #9:

> Comma separated lists are already documented here:
> https://octave.org/doc/v6.2.0/Comma-Separated-Lists.html
> And how a cell array can be converted to a c-s-list is documented here:
> https://octave.org/doc/v6.2.0/Indexing-Cell-Arrays.html
>
> If we want to expand on that documentation, we should probably do that in those more general sections (not for each function that can be called with c-s-lists -- which would basically be all functions).
>
>


I had no idea that cells expanded to comma separated lists could be used in function arguments as a series of function arguments. This opens so many doors.

Thank you!

N Kando <itskando>
Mon 08 Mar 2021 06:12:36 PM UTC, comment #11: 

I pushed a small change to the documentation of `save` here:
https://hg.savannah.gnu.org/hgweb/octave/rev/cb2d14b384df

Report was already closed. Changing status to fixed.

Markus Mützel <mmuetzel>
Project Member
Mon 08 Mar 2021 05:46:12 PM UTC, comment #10: 

For those who are unfamiliar with regular expressions, strcmp() might be an easier way to implement the EXCEPT method.

Example:

var_list = who ();
delete_idx = strcmp ("ans", var_list) | strcmp ("cat", var_list);
var_list(delete_idx) = [];
save ('filename', var_list{:})

Rik <rik5>
Project Administrator
Mon 08 Mar 2021 05:38:51 PM UTC, comment #9: 

Comma separated lists are already documented here:
https://octave.org/doc/v6.2.0/Comma-Separated-Lists.html
And how a cell array can be converted to a c-s-list is documented here:
https://octave.org/doc/v6.2.0/Indexing-Cell-Arrays.html

If we want to expand on that documentation, we should probably do that in those more general sections (not for each function that can be called with c-s-lists -- which would basically be all functions).

Markus Mützel <mmuetzel>
Project Member
Mon 08 Mar 2021 03:53:19 PM UTC, comment #8: 

comment #7:

> You could use the following regular expression:

> save('filename.mat', who('-regexp', '^((?!\bans\b|\bcat\b).*)$'){:})


Oh.

I was not aware that {:} expansion could be used to place a cell in save(). I had only tried including cells without that and assumed they were not compatible.

Request to mention or include your example of this along with a `see also` reference to `who()`, as the two functions work incredibly well together.

save('filename.mat', who('-regexp','.'){:})

N Kando <itskando>
Mon 08 Mar 2021 03:36:55 PM UTC, comment #7: 

You could combine `who` with `save`.
For your example, you could use the following regular expression:

save('filename.mat', who('-regexp', '^((?!\bans\b|\bcat\b).*)$'){:})

If you need something like this regularly, you could probably write a function that creates the regular expression automatically from a cell string.

Markus Mützel <mmuetzel>
Project Member
Mon 08 Mar 2021 02:51:51 PM UTC, comment #6: 

Also, in the present implementation, is there any means to save all variables EXCEPT for a specifically named one through just the save function?

As a simple example, if I have a list of variables, and I want to save everything except 'ans' or 'cat' but I still need to save 'a' and 'c', what would be the input?

N Kando <itskando>
Mon 08 Mar 2021 02:40:10 PM UTC, comment #5: 

I think referring to "wildcard / glob patterns" would help by providing more buzzwords with which the user may search and familiarize.

I additionally believe that allowing the user to provide a cell vector of string matrices listing all of the variables to be saved could allow much greater flexibility with the `save()` command, particularly due to the existence of the `who` command as well as the existence of `-regexp` functionality within the `who` command.

Can I post the desire to allow for cell inputs for the `save()` command as a feature request?

N Kando <itskando>
Sun 07 Mar 2021 12:29:32 PM UTC, comment #4: 

Is "wildcard (glob) pattern" something that is understood widely enough that it could be helpful to add it to the documentation in this case?
See also this Wikipedia article:
https://en.wikipedia.org/wiki/Glob_(programming)

Markus Mützel <mmuetzel>
Project Member
Tue 02 Mar 2021 10:43:24 PM UTC, comment #3: 

Regular expressions aren't supported and that is why they are not mentioned.  I only referenced them as a way of understanding how the [ LIST ] feature works.  The documentation is correct that this is the complete list of accepted wildcard characters.

Rik <rik5>
Project Administrator
Tue 02 Mar 2021 10:08:46 PM UTC, comment #2: 

But the documentation never even mentions regular expressions.

Only from familiarity with matlab's save('-regexp') option did I associate regular expressions with octave's save() function. I thought octave's save() function might only have tools which offer a limited set of commands which parallel regexp, because the documentation is unclear unless (or even if) the user has prior knowledge.

I had also looked up LIST to see if there was any common usage detailing the syntax of what could be input, but there's no identifying words of what could go here.

Here's the whole section:

     The list of variables to save may use wildcard patterns containing
     the following special characters:

     '?'
          Match any single character.

     '*'
          Match zero or more characters.

     '[ LIST ]'
          Match the list of characters specified by LIST.  If the first
          character is '!' or '^', match all characters except those
          specified by LIST.  For example, the pattern '[a-zA-Z]' will
          match all lower and uppercase alphabetic characters.

          Wildcards may also be used in the field name specifications
          when using the '-struct' modifier (but not in the struct name
          itself).

"The list of variables to save may use wildcard patterns containing the following special characters" makes it sound like the options provided are an exhaustive list.

If regular expressions can be used to refer to anything in the workspace, I believe the documentation should be edited to make that clear and to suggest the user review the regexp() command for more information.

N Kando <itskando>
Tue 02 Mar 2021 09:48:34 PM UTC, comment #1: 

For extra documentation, try searching for "character classes" in combination with "regular expressions".  There is a lot out there.

To start with, create some variables

clear all
a1 = 1;
a2 = 2;
a3 = 3;
b1 = 21;
b2 = 22;
b3 = 23;
c1 = 31;
c2 = 32;
c3 = 33;

Here are some various documented combinations using the variables above.

# save all 'a' variables (a1, a2, a3)
save tmp a*
# save all variables of the form X1 where X can be any char (a1, b1, c1)
save tmp ?1
#save all variables X2 where X is any lowercase alphabetic char (a2, b2, c2)
save tmp [a-z]2
#save all variables X3 where X is not 'b' (a3, c3)
save tmp [^b]3
#same thing, but using '!' specifier (a3, c3)
save tmp [!b]3
#save a2,a3,c2,c3 using combination of LIST and LIST RANGES
save tmp [ac][2-3]

The modifiers like '!', '^' need to appear INSIDE the LIST.  The other modifiers '?', '*' should appear OUTSIDE the LIST.

Rik <rik5>
Project Administrator
Tue 02 Mar 2021 08:16:49 PM UTC, original submission:  

The help documentation regarding save() includes the following:

     '[ LIST ]'
          Match the list of characters specified by LIST.  If the first
          character is '!' or '^', match all characters except those
          specified by LIST.  For example, the pattern '[a-zA-Z]' will
          match all lower and uppercase alphabetic characters.

          Wildcards may also be used in the field name specifications
          when using the '-struct' modifier (but not in the struct name
          itself).

What is the syntax of LIST? Is it possible to exclude certain patterns? I was not able to make the 'except' characters work:

>> clear
>> 1
ans = 1
>> save('trash.txt','a*')
>>
>> save('trash.txt','a')
warning: save: no such variable 'a'
>> save('trash.txt','[a]')
warning: save: no such variable '[a]'
>> save('trash.txt','[!a*]')
warning: save: no such variable '[!a*]'
>> save('trash.txt','[!b*]')
warning: save: no such variable '[!b*]'
>> save('trash.txt','![b]')
warning: save: no such variable '![b]'
>> save('trash.txt','!b')
warning: save: no such variable '!b'

N Kando <itskando>

 

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

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by mmuetzel (Posted a comment)
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by itskando (Submitted the item)
  • -email is unavailable- added by itskando
  •  

    Do you think this task is very important?
    If so, you can add your encouragement to it.
    This task has 0 encouragements so far.

    Only project members can vote.

     

     

     

    Follow 5 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2021-03-08 mmuetzel StatusInvalid => Fixed
    2021-03-07 mmuetzel CategoryOctave Function => Documentation
    2021-03-02 rik5 StatusNone => Invalid
        Open/ClosedOpen => Closed
    2021-03-02 itskando Carbon-Copy- => Added nate.kando@gmail.com

    Back to the top


    Powered by Savane 3.7