STRCAT is not compatible

Peter Cloetens cloetens at esrf.fr
Thu Feb 21 02:59:21 CST 2008



Tribo Laboy wrote:
> Hello,
> 
> While porting some ML code to octave I found that STRCAT in Octave
> 3.0.0 is not compatible to ML 2006a.
> Octave behaviour is as documented in the manual, so strictly speaking
> this is not a bug.
> 
> In summary, Matlab allows one of the arguments to be a cell array of
> strings, or both to be cell arrays if they have equal size.
> 
> These two examples illustrate Matlab behaviour:
> 
>>>  ca = {'a','b','c'};
>>>  s = 'xyz';
>>> strcat(s,ca)
> 
> ans =
> 
>     'xyza'    'xyzb'    'xyzc'
> 
>>> strcat({'Red','Yellow'},{'Green','Blue'})
> 
> ans =
> 
>     'RedGreen'    'YellowBlue'
> 
> 
> In Octave 3.0.0 these examples result in errors:
> 
>>> ca = {'a','b','c'};
>>> s = 'xyz';
>>> strcat(s,ca)
> error: strcat: all arguments must be strings
> error: evaluating if command near line 38, column 3
> error: called from `strcat' in file `D:\PORT\PortApps\MATH\qtoctave\Octave\shar
> e\octave\3.0.0\m\strings\strcat.m'
> 
>>> strcat({'Red','Yellow'},{'Green','Blue'})
> error: strcat: all arguments must be strings
> error: evaluating if command near line 38, column 3
> error: called from `strcat' in file `D:\PORT\PortApps\MATH\qtoctave\Octave\shar
> e\octave\3.0.0\m\strings\strcat.m'
> 
> Just in case it is needed, I am running Octave under Win XP.
> _______________________________________________
> Bug-octave mailing list
> Bug-octave at octave.org
> https://www.cae.wisc.edu/mailman/listinfo/bug-octave

Hello,
I encountered this compatibility problem before and wrote the simple 
function below to circumvent it.
It handles the different combinations:
string - string
cell array - string
string - cell array
cell array - cell array
Peter


function res = mystrcat(obj1,obj2)
     if (ischar(obj1) & ischar(obj2))
         res = strcat(obj1,obj2);
     elseif (ischar(obj1) & iscell(obj2))
         for k = 1:length(obj2)
             res{k} = strcat(obj1,obj2{k});
         end
     elseif (iscell(obj1) & ischar(obj2))
         for k = 1:length(obj1)
             res{k} = strcat(obj1{k},obj2);
         end
     elseif (iscell(obj1) & iscell(obj2))
         if (length(obj1) == length(obj2))
             for k = 1:length(obj1)
                 res{k} = strcat(obj1{k},obj2{k});
             end
         else
             res = [];
         end
     else
         res = [];
     end



More information about the Bug-octave mailing list