bugGNU Octave - Bugs: bug #52488, genvarname substitutes underscore...


bug #52488: genvarname substitutes underscore for invalid characters in string

Submitted by:  None
Submitted on:  Thu 23 Nov 2017 04:16:14 PM UTC  
Category:  Octave Function Severity:  2 - Minor
Priority:  1 - Later Item Group:  Matlab Compatibility
Status:  Wont Fix Assigned to:  None
Originator Name:  Motius Originator Email:  -email is unavailable-
Open/Closed:  Closed Release:  dev
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)

Thu 13 Dec 2018 07:16:56 AM UTC, comment #7: 

Matlab has made genvarname a legacy function and it will be removed at some point in the future.  Octave now has matlab.lang.ValidName which allows a choice for the replacement.  Marking as "Won't Fix" and closing report.

Rik <rik5>
Project Administrator
Fri 19 Jan 2018 03:00:39 AM UTC, comment #6: 

Submitting a changeset to fix this bug.  I initially made the parameter default to "" for Matlab compatibility but realized this would then require all existing m-files and tests to possibly need updates, so I've set the default to "_" for now.  Let me know what you all think.

(file #42971)

Stevie Schraudner <stevietodd>
Sat 25 Nov 2017 12:18:55 PM UTC, comment #5: 

genvarname is an .m-file function so it should be easy to add some option in the input, maybe as an optional third argument.
Could be something like "" (= do like Matlab) or (default?) "_" (= replace invalid chars by underscores).

I have no time for it now but after perusing genvarname.m this looks like a nice little project for a novice contributor.

Release => dev
OS => any

Philip Nienhuis <philipnienhuis>
Project Member
Fri 24 Nov 2017 09:33:33 AM UTC, comment #4: 

How about providing a choice for the user?
Something along these lines:

  • default behavior is to strip invalid characters (as does Matlab)
  • provide an option to override with a substitute character (I do agree with Rik that underscores make the name easier to read, but this breaks Matlab code...)
Motius <motius>
Thu 23 Nov 2017 09:09:54 PM UTC, comment #3: 

True, it would be easy to call strtrim on the inputs.  But, I figured since Octave was already not 100% compatible it didn't matter much.

Rik <rik5>
Project Administrator
Thu 23 Nov 2017 08:39:01 PM UTC, comment #2: 

One could argue that trailing spaces cn be removed rather than replaced by underscores.

Philip Nienhuis <philipnienhuis>
Project Member
Thu 23 Nov 2017 05:19:41 PM UTC, comment #1: 

According to Matlab's documentation,

varname = genvarname(str) constructs a string or character vector varname that is similar to or the same as the str input, and can be used as a valid variable name.

The only requirements are that the name be "similar" and that the name is a valid syntax for the interpreter.

Although Octave doesn't remove spaces as Matlab does, it does produce a valid variable name that is "similar" to the original.  It meets the requirements of the function genvarname, and I prefer the use of underscores as I think it makes the generated names easier to read.  Imagine you have a variable name that encodes something about a measurement in the name.

var = 'Site 1 laboratory 5 centrifuge 3 RPM';
genvarname (var)
ans = Site_1_laboratory_5_centrifuge_3_RPM
ans = Site1laboratory5centrifuge3RPM

The Matlab name is very difficult to decipher.

Rik <rik5>
Project Administrator
Thu 23 Nov 2017 04:16:14 PM UTC, original submission:  


The following snippet illustrates diverging behaviors between Octave and MatLab


genvarname("foo ")
genvarname('bar ')


Gives the following result under Octave:


and the following one under MatLab:



Tested with Octave :

  • v4.0.3;
  • v4.2.1 (1a8b7b3ae5f2);

and MatLab v2017a


diff -r cf33c1b910b7 scripts/miscellaneous/genvarname.m
--- a/scripts/miscellaneous/genvarname.m        Tue Nov 21 22:00:45 2017 -0800
+++ b/scripts/miscellaneous/genvarname.m        Thu Nov 23 16:49:24 2017 +0100
@@ -130,7 +130,7 @@
     ## a valid variable name.

     ## remove invalid characters
-    str{i}(! (isalnum (str{i}) | str{i} == "_")) = "_";
+    str{i}(! (isalnum (str{i}) | str{i} == "_")) = [];
     ## do not use keywords
     if (iskeyword (str{i}))
       firstcharacter = toupper (str{i}(1));

is a suggested patch.



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

Attach Files:

Attached Files
file #42971:  52488.cset added by stevietodd (4KiB - application/octet-stream)


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -email is unavailable- added by stevietodd (Updated the item)
  • -email is unavailable- added by motius (Posted a comment)
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by None (Submitted the item)

    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 9 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2018-12-13 rik5 StatusConfirmed => Wont Fix
        Open/ClosedOpen => Closed
    2018-01-19 stevietodd Attached File- => Added 52488.cset, #42971
    2017-11-25 philipnienhuis Release4.2.1 => dev
        Operating SystemGNU/Linux => Any
    2017-11-23 rik5 Summarygenvarname substitutes underscore to invalid characters in string => genvarname substitutes underscore for invalid characters in string
    2017-11-23 rik5 Severity3 - Normal => 2 - Minor
        Priority5 - Normal => 1 - Later
        StatusNone => Confirmed

    Back to the top

    Powered by Savane 3.5