stringmatlabjoinoctavecompatibility

strjoin compatibility in Matlab and Octave


For the function strjoin, it would be good the same code could run on Matlab and Octave. The parameters for strjoin are almost the same. The difference is in Matlab it requires a vector [], and in Octave it requires a cell {}. Is there any better way than to check the "OCTAVE_VERSION" to switch?

strjoin(["mat" "lab"]);

https://www.mathworks.com/help/matlab/ref/strjoin.html

strjoin({"oc" "tave"});

https://octave.sourceforge.io/octave/function/strjoin.html

OCTAVE_VERSION

https://docs.octave.org/v4.0.3/How-to-distinguish-between-Octave-and-Matlab_003f.html


Solution

  • strjoin in MATLAB also takes a cell array of character vectors, as you can read in the documentation you linked.

    In MATLAB, traditionally, a “string” was a character vector (an array where the elements are characters). These are created with ': 'foo'. To put more than one together in the same variable, you need to put them in a cell array. Octave, seeing that " was unused in MATLAB syntax, took it to mean the same thing as '. Hence you can create a character vector in Octave as 'foo' or as "foo".

    Later, MATLAB introduced the string object. This is created with " ("foo"), and can be made into an array (a string array) with [] just like numbers. Indexing works differently for this object, and there are many advantages in usability. Octave has no way of replicating this without breaking a lot of existing Octave code, so there is no string in Octave.

    Thus, if you want to make code that works in both Octave and MATLAB, always use the single quotes ' for your strings. strjoin({'foo' 'bar'}) works in both.