Appending mixed types to strings seems a bit tricky. Is this the intended behaviour?
2 views (last 30 days)
Rik on 27 Sep 2022
Edited: Rik on 27 Sep 2022
It helps to do this step by step, which show that (while unintuitive), this is intended behavior.
Matlab evaluates code from left to right:
'1' + "1"
ans + 1
And your second example:
'1' + 1
ans + "1"
Because the first call to plus doesn't know you want to use a string as an operand later, this results in a conversion to double, and since the UTF-16 encoding of '1' is uint16(49), this results in double(50).
You can avoid this by putting in some parentheses:
'1' + ( 1 + "1" )