Matlab compatibilty [changeset] for strcat

Ben Abbott bpabbott at mac.com
Wed Sep 24 19:40:57 CDT 2008


On Sep 24, 2008, at 2:42 PM, John W. Eaton wrote:

> On 24-Sep-2008, Ben Abbott wrote:
>
> |
> | On Sep 23, 2008, at 10:50 PM, Ben Abbott wrote:
> |
> | > On Sep 23, 2008, at 9:20 PM, Erich Ritz wrote:
> | >
> | >> I tried searching to see if this issue was already posted;  
> forgive
> | >> me if it is.
> | >>
> | >> Matlab allows:
> | >>
> | >> my_str = strcat([], 'a')
> | >>
> | >> and octave gives an error: error: strcat: all arguments must be
> | >> strings
> | >>
> | >> The rational for the above statement is:
> | >>
> | >> my_str = [];
> | >> for ...
> | >>   ...
> | >>   my_str = strcat(my_str, 'some text');
> | >> end
> | >>
> | >> The work around is to do:
> | >> my_str = '';
> | >> instead when initializing.
> | >>
> | >> Note also that octave allows
> | >> my_str = [my_str 'some text'];
> | >> in the above context, but [] behave differently than strcat().
> | >>
> | >> Erich
> | >
> | > hmmm ... another example. Running Matlab ...
> | >
> | > 	>> strcat (33, 33)
> | > 	ans =
> | > 	!!
> | > 	
> | > Even stranger, real numbers are round down (floor)
> | >
> | > 	>> strcat (33.7, 33)
> | > 	ans =
> | > 	!!
> | >
> | > I guess all positive reals work going in, but return characters  
> in all
> | > cases.
> | >
> | > 	>> strcat(-19,33)
> | > 	Warning: Out of range or non-integer values truncated during
> | > conversion to character.
> | > 	ans =
> | > 	 !
> | >
> | > Even, out of range numbers give a warning, but no error.
> | >
> | > This should be rather easy to fix. If no one gets to it, I'll  
> take a
> | > look later tomorrow.
> | >
> |
> | I've created a short change set for strcat to respect the Matlab
> | functionality. A pair of tests are included.
>
> OK.
>
> | As char(-19) already gives a warning similar to Matlab, and  
> appears to
> | work in a compatible manner, this was quite simple.
> |
> | 	>  x = char(-19)
> | 	warning: range error for conversion to character value
>
> It doesn't quite work in a compatible way.  In Matlab, it appears that
> strcat (-19, 33) generates the character string with the two elements
> char(0) and char(33).  It seems that the use of cellstr in strcat is
> what is causing the problem.  Calling cellstr (char (-19)) produces
> {''}, not {char(0)} (and that appears to be compatible behavior, so
> the cellstr function is not the place for the fix.
>
> I checked in the change anyway as it does improve compatibility.  But
> it would be nice if this other minor difference could also be fixed.

I'm looking at it, but discovered a bug in the process. Running 3.1.51+

	real(char(1:255))

returns 1, 2, 3, 4, ... 127, -128, -127, ... 1

I've already emailed the bug-octave.

Ben




More information about the Bug-octave mailing list