2

I am aware that in octave escape sequences are treated differently in single/double quotes. Nevertheless, there seems to be a type difference:

Whereas class("bla") and class('bla') are both char, typeinfo("bla") is string, whereas typeinfo('bla') is sq_string, which may be short for single quote string.

More interesting, warning("on", "Octave:mixed-string-concat") activates warning that these two types are mixed. So after activation, ["bla" 'bla'] yields a warning.

Note that typeinfo(["bla" "bla"]) is string, whereas if one of the two strings concatenated is single quote, so is the result, e.g. typeinfo(['bla' "bla"]) is sq_string.

I have a situation where someone activates the warning and so I want to program so to avoid these. Thus my question: is there a way to convert sq_string to string?

The core of my problem is that fieldnames seem to be single quoted strings.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
user2609605
  • 419
  • 2
  • 14
  • 1
    I don’t have an answer, but I have a bit of history: MATLAB did strings (char vectors) as `'`, and didn’t recognize `"`. Octave wanted to improve on MATLAB, and allowed both types of quotes, doing exactly the same thing. Now MATLAB has expanded its language to have the double quote be an actual string, you can make string arrays (`["foo" "bar"]` is an array of two strings). But of course Octave cannot change its definition of the double quote, so cannot directly implement these strings. I’m guessing they’re slowly transitioning to having string arrays? – Cris Luengo Jan 06 '23 at 22:10

1 Answers1

0

What an interesting question. I've never thought one might have a need for such a warning or conversion ... though now that I think about it, it makes sense if you want to collect 'raw' strings, and have their escape sequences interpreted and vice versa ...

After some experimentation, I have found a way to do what you want: use sprintf. This seems to return a (double-quoted) string if your formatted string is in double quotes, and an sq_string if it's in single quotes. If your formatted string is simply "%s", then you can pass a bunch of strings as subsequent arguments, and these will be concatenated (as a double-quoted string).

If you'd prefer to go in the reverse direction and ensure your strings are always single quoted, you can then still do the above with a single-quoted formatted string, or you can just use strcat: this does not trigger your warning, can also be called with a single argument, and seems to always return an sq_string.

Also, since I would generally recommend using either of these with "cell-generated sequence" syntax for convenience, this means that you would be better off "collecting" individual strings in cells more generally. E.g.

a = { 'one', 'two', 'three' }
b = { "four", "five", "six" }
typeinfo( sprintf( "%s", a{:} ) )   % outputs: string
typeinfo( strcat( b{:} ) )          % outputs: sq_string
Tasos Papastylianou
  • 21,371
  • 2
  • 28
  • 57
  • Adding as a comment just to say, I get the impression most string-returning functions seem to return sq_string. Whether this is by convention and/or consistent, I don't know. But I would prefer sq_strings to dq_strings more generally, except when you're really sure you want an escape sequence interpreted; and I would only convert to this at the final point of use. Also, you're probably aware of this already, but octave provides is_dq_string and is_sq_string functions for testing string types directly. May I ask what your use case is for mixed strings btw? I'm curious. – Tasos Papastylianou Jan 07 '23 at 10:06