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 FunctionSeverity: 2 - Minor
Priority: 1 - LaterItem Group: Matlab Compatibility
Status: ConfirmedAssigned to: None
Originator Name: MotiusOriginator Email: -unavailable-
Open/Closed: OpenRelease: 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 Jump to the original submission)

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,

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.

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


is a suggested patch.



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

Attach File(s):

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
  • -unavailable- added by stevietodd (Updated the item)
  • -unavailable- added by motius (Posted a comment)
  • -unavailable- added by rik5 (Posted a comment)
  • -unavailable- added by None (Submitted the item)

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

    Only project members can vote.


    Please enter the title of George Orwell's famous dystopian book (it's a date):



    Follow 7 latest changes.

    Date Changed By Updated Field Previous Value => Replaced By
    Fri 19 Jan 2018 03:00:39 AM UTCstevietoddAttached File-=>Added 52488.cset, #42971
    Sat 25 Nov 2017 12:18:55 PM UTCphilipnienhuisRelease4.2.1=>dev
      Operating SystemGNU/Linux=>Any
    Thu 23 Nov 2017 05:20:15 PM UTCrik5Summarygenvarname substitutes underscore to invalid characters in string=>genvarname substitutes underscore for invalid characters in string
    Thu 23 Nov 2017 05:19:41 PM UTCrik5Severity3 - Normal=>2 - Minor
      Priority5 - Normal=>1 - Later

    Back to the top

    Powered by Savane 3.1-cleanup1