strrep is inconsistent with empty replacement strings

5 Ansichten (letzte 30 Tage)
Ian
Ian am 20 Mai 2019
Kommentiert: Stephen23 am 5 Jun. 2019
I have reported this as a bug. strrep responds differently when the replacement string is various flavors of empty.
'Proper' ways to do this:
x = "abc defg";
>> y = strrep(x,"defg","") % with an empty string object
y =
"abc " % expected result
>> y=strrep(x,'defg','') % with an empty char array
y =
"abc " % same result
y=strrep(x,'defg',char([])) % with an empty char array
y =
"abc " % also same result
One might be tempted to use [ ] or string([ ]) as the replacement string, especially if working with something like a class member that is initialized to empty. But using the first produces the expected result (but with a warning), while using the second unexpectedly clears the entire return value:
>> y = strrep(x,"defg", []) % with an empty array
Warning: Inputs must be character vectors, cell arrays of character vectors, or string arrays.
y =
"abc " % same result, but with warning...
>> y = strrep(x,"defg", string([])) % with an empty string array
y =
0×0 empty string array % ouch! why is this one different?
It would be nice in Matlab would either treat [ ] and string([ ]) the same as "", '' and char( [ ] ), or throw an error so code aborts when handed an unexpected data type.

Antworten (1)

Shivam Sardana
Shivam Sardana am 29 Mai 2019
string([])” is an empty string array, not a scalar string array with no characters in it. strrep retains the shape of its non-scalar inputs. To get the expected result i.e. "abc ", provide a scalar string with no characters in it:
y = strrep(x,"defg", string(['']))
  1 Kommentar
Ian
Ian am 4 Jun. 2019
Bearbeitet: Ian am 4 Jun. 2019
That is not the issue here. Matlab sees [ ], char([ ]) and string([ ]) as empty objects, so one should reasonably expect Matlab to use them identically in a character-processing operation. However, it responds differently to the latter one than it does to the former two. In my post above, you can see that using
string( [ ] )
as a parameter erroneously results in the output being completely empty, while using just
[ ]
or
char( [ ] )
results in the expected output. String objects and char arrays are interchangeable in most circumstances in matlab, so strrep(...) should either treat all three identically, or throw an error if using an empty string objct is not valid input.
The problem here is that Matlab's relatively recent addition of string objects is an ongoing process, and still needs a bit of cleaning up. Mathworks has not yet found all the places where it is silently treating string objects differently from char arrays.
Pointing out these inconsistencies will help Mathworks find and address these issues. I have submitted this to Mathworks as a bug report, and they agreed that there is an inconsistency here which needs fixing.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by