bugGNU Octave - Bugs: bug #50855, new Matlab string array syntax and...


bug #50855: new Matlab string array syntax and string class

Submitter:  Ceral Paquet <octavebugs>
Submitted:  Sat 22 Apr 2017 04:36:35 PM UTC
Category:  Interpreter Severity:  2 - Minor
Priority:  1 - Later Item Group:  Feature Request
Status:  Confirmed Assigned to:  None
Originator Name:  Open/Closed:  * Open
Release:  * dev Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup

Jump to the original submission

Fri 18 Nov 2022 08:04:31 PM UTC, comment #11: 

I made the change suggested in comment #10 to future proof the isstring.m function for the day when the string class is actually implemented.  See http://hg.savannah.gnu.org/hgweb/octave/rev/caf42f5c3f4e.

Rik <rik5>
Group administrator
Tue 01 Nov 2022 09:35:44 PM UTC, comment #10: 

Noticing a string class was again discussed in the last developer meeting:


A particular focus was on the string class (again). It might be a candidate for an early addition during the development cycle of Octave 9 (taking leverage of @apjanke’s .m file implementation in the tablicious package). Having that might allow adapting a range of functions to accepting string objects early on (even if a “final” implementation of the string class might be in C++). Decisions for how to transition to the new syntax for double quotes still need to be taken.

I think adding the functions from comment #8 would already be useful for compatibility with Matlab code. Function isstring () could also be modified to be ready for a string class:

-  tf = false;
+  tf = isa (s, "string");

Also attaching my earlier work on a string class with BISTs. Methods needed to be implemented are commented out in the file. They should probably better be added as separate files, e.g., @string/matches.m.

(file #53924)

Guillaume <gyom>
Sat 26 Jun 2021 01:05:15 PM UTC, comment #9: 

I pushed the patch to add `__unicode_length__` here:

Markus Mützel <mmuetzel>
Group administrator
Fri 11 Jun 2021 09:13:18 AM UTC, comment #8: 

@Markus: thanks for this. Could your patch be pushed in Octave? I guess "unicode_idx" could be renamed "__unicode_idx__" too.

Very low hanging fruit but I attach possible implementations of convertStringsToChars, convertContainedStringsToChars, convertCharsToStrings and isStringScalar. They, of course, only make sense when the string class will be implemented (esp. convertCharsToStrings) but, like isstring, they can already be useful for compatibility with Matlab.
There is one issue with curly brackets in the help text (example section) but I am not sure how to fix it. Octave's documentation currently already refers to 'strings' when meaning char arrays or row char vectors (see e.g. strfind) -- I tried to be more explicit for the help text of these functions but it might still require perfecting.

(file #51543, file #51544, file #51545, file #51546)

Guillaume <gyom>
Sun 02 May 2021 04:18:49 PM UTC, comment #7: 

Attached is a patch that adds a helper function "__unicode_length__" that could be used by the string class "strlength" method and a function of the same name that could work on character arrays and cell strings.

See also this Discourse thread: https://octave.discourse.group/t/implementation-of-a-string-class/1089

(file #51357)

Markus Mützel <mmuetzel>
Group administrator
Fri 11 Jan 2019 11:59:46 PM UTC, comment #6: 

Gotcha. Thanks.

For the sake of discussion, here's an example of how a @string array could be implemented as a userland M-code class built on top of cellstrs: https://savannah.gnu.org/bugs/download.php?file_id=45939

It supports lexical casting and various other type conversions.

This approach works decently for string data analysis by users: it's a usable datatype, and functions aimed at string manipulation will work well on it. For text data sets, a user can create @string arrays and work with them usefully.

Where it works less well is all the various functions that take strings as incidental arguments, like the plotting functions, property getter/setters, statistical functions that take name/value pair arguments, and all the various other miscellaneous functions. Packages (e.g. from Octave Forge) will also not be @string-aware unless explicitly coded to be so.

Also, since this is based on cellstrs, the underlying "character" semantics are still those of Octave chars. (That is, bag-of-bytes interpreted via context or user locale settings? I'm not actually sure exactly how Octave chars work.)

Andrew Janke <apjanke>
Fri 11 Jan 2019 08:12:18 PM UTC, comment #5: 

Andrew - by "lexical casting" I meant conversion between numeric and string types that interprets the numeric value that the string represents, rather than the byte values of the characters.

Most interpreted languages use lexical casting for converting between strings and numerics. If you're familiar with Python, then using 'int' and 'str' to convert between the two is lexical casting, while using 'chr' and 'ord' imitates a C-style byte-oriented casting.

I was only pointing out that the string type has different rules for conversion between string and double than the char type that we support in Octave now, so it will need a completely new class definition and type support to differentiate it.

Mike Miller <mtmiller>
Group Member
Fri 11 Jan 2019 12:39:56 PM UTC, comment #4: 

> String array is a fundamental type, not a cell array, and follows different rules for lexical casting from other types and so on.

Could you elaborate on that a bit? What is "lexical casting"?

Andrew Janke <apjanke>
Mon 24 Apr 2017 02:02:05 AM UTC, comment #3: 

Of course even before this syntax change could possibly be adopted, we need a working implementation of the string array type itself. That can be done with or without the change in how the double quote character is interpreted by Octave.

String array is a fundamental type, not a cell array, and follows different rules for lexical casting from other types and so on.

Mike Miller <mtmiller>
Group Member
Sun 23 Apr 2017 02:22:50 PM UTC, comment #2: 

In terms of workarounds, it is quite easy not to use this specific feature of Matlab if you want to exchange scripts between the two environments.  Just use the regular cell string construction operator if you do, in fact, want a cell array.

str = {'Good' 'Morning'}

Rik <rik5>
Group administrator
Sun 23 Apr 2017 02:06:45 PM UTC, comment #1: 

I saw that.  I don't know if they were being ornery and trying to force Octave to spend developer time catching up, or if they had simply run out of characters on a standard keyboard.

Rik <rik5>
Group administrator
Sat 22 Apr 2017 04:36:35 PM UTC, original submission:  

Not sure if you guys noticed but MATLAB recently introduced String arrays which may conflict with Octave's use of the double-quote "" versus ''.


str = ["Good" "morning"]

now produces a valid 1x2 string array.

Ceral Paquet <octavebugs>


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

Attach Files:

Attached Files
file #53924:  string.m added by gyom (25KiB - text/x-objcsrc)
file #51544:  convertContainedStringsToChars.m added by gyom (4KiB - text/x-objcsrc)
file #51545:  convertCharsToStrings.m added by gyom (3KiB - text/x-objcsrc)
file #51546:  isStringScalar.m added by gyom (2KiB - text/x-objcsrc)
file #51543:  convertStringsToChars.m added by gyom (4KiB - text/x-objcsrc)
file #51357:  bug50855_unicode_length.patch added by mmuetzel (4KiB - application/octet-stream)
file #45939:  string.m added by apjanke (21KiB - application/octet-stream - rough example of an M-code @string implementation)


Carbon-Copy List
  • -email is unavailable- added by gyom (Updated the item)
  • -email is unavailable- added by mmuetzel (Updated the item)
  • -email is unavailable- added by apjanke (Posted a comment)
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by octavebugs (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 22 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2022-11-01 gyom Attached File- Added string.m, #53924
    2021-06-26 mmuetzel Summarynew Matlab string array syntax new Matlab string array syntax and string class
    2021-06-11 gyom Attached File- Added convertStringsToChars.m, #51543
        Attached File- Added convertContainedStringsToChars.m, #51544
        Attached File- Added convertCharsToStrings.m, #51545
        Attached File- Added isStringScalar.m, #51546
    2021-05-08 mmuetzel Dependencies- bugs #60555 is dependent
    2021-05-03 mtmiller Carbon-CopyRemoved 80942 -
    2021-05-02 mmuetzel Attached File- Added bug50855_unicode_length.patch, #51357
        Item GroupWTF, Matlab?!? Feature Request
        Release4.2.1 dev
        Operating SystemGNU/Linux Any
    2020-12-21 mmuetzel Dependencies- bugs #59725 is dependent
    2020-11-03 mmuetzel Dependencies- bugs #59389 is dependent
    2020-03-28 mtmiller Dependencies- bugs #58071 is dependent
    2019-01-11 apjanke Attached File- Added string.m, #45939
    2018-03-23 mtmiller Dependencies- bugs #53439 is dependent
    2017-04-23 rik5 CategoryNone Interpreter
        Severity3 - Normal 2 - Minor
        Priority5 - Normal 1 - Later
        StatusNone Confirmed
        SummaryString array new Matlab string array syntax

    Back to the top

    Powered by Savane 3.13-04b1.
    Corresponding source code