Escape characters in compose with array input and minus sign *edit* with "empty" string ""

조회 수: 14 (최근 30일)
I have trouble understanding the conditions under which compose() works when passing combination of "empty" strings "", escape characters, and normal stuff that you would send to compose:
q = compose("%s %d\\gamma","",4) % i understand this works, and I am fine to implement in my code
q = " 4\gamma"
q = compose("%s%d\\gamma"," ",4) % i understand this works, and I am fine to implement in my code
q = " 4\gamma"
try
q = compose("%s%d\\gamma","",4) % why doesn't this work?
catch ME
ME
end
ME =
MException with properties: identifier: 'MATLAB:printf:BadEscapeSequenceInFormat' message: 'Escaped character '\g' is not valid. See 'doc sprintf' for supported special characters.' cause: {} stack: [3×1 struct] Correction: []
It is still not really an issue for my ultimate goal, as I could rely later on trim() etc., but I posted because it was not easy for me to arrive at the example that actually works because the problem was not obvious to me - and it still isn't despite the hint by dpb's first answer, which identifies the "empty" string as potentially culprit.
q = compose("%s%d","",4) % not an issue with "" here
q = "4"
q = compose("%s\\gamma","") % not an issue with "" even if its right next to escape character
q = "\gamma"
So it doesn't appear to be an issue with passing "" with escape characters, per se...
Some more experiments that illustrate but don't help me understand:
q = compose("%s\\gamma",compose("%s%d","",4)) % this works
q = "4\gamma"
q = compose("%s%s","",compose("%d\\gamma",4)) % this works
q = "4\gamma"
q = compose("%s%s\\gamma","4","") % this works, but doesn't matter for my application because its reversed
q = "4\gamma"
q = compose("%s%s%s","","4","\gamma") % this works, and is potentially the most intuitive solution that works for me
q = "4\gamma"
q = compose("%s%s\\gamma","","4") % this does not work
Error using compose
Escaped character '\g' is not valid. See 'doc sprintf' for supported special characters.
****** original post below when I thought the issue had to do with arrays, which thanks to dpb's answer, it is clearly not:
I am having trouble using "compose()" with arrays, escape characters, and a minus sign at the same time
Compose with arrays with minus signs
s = compose("%s%d/%d",["";"-";"-";""],transpose(1:4),transpose(5:8))
Compose with escape characters with minus sign and scalars
r = compose("%s%d\\gamma/%d",["-"],5,6)
But apparently I cannot combine these 2 things I want to do unless I put a space between the minus sign and the next thing; also no problem with just a minus sign in the format string
q = compose("%s %d\\gamma/%s",["";"-";"-";""],transpose(1:4),compose("%d",transpose(5:8)))
q = compose("-%d\\gamma/%s",transpose(1:4),compose("%d",transpose(5:8)))
q = compose("%s%d\\gamma/%s",["";"-";"-";""],transpose(1:4),compose("%d",transpose(5:8)))
Not a big problem for what i want to ultimately do, but I wasted nearly an hour figuring this out and curious what I'm missing - nothing in the doc for compose helps me understand this...
  댓글 수: 9
J. Alex Lee
J. Alex Lee 2023년 9월 25일
@dpb, yes i submitted one, significantly simplified and focused than my original question, thanks to your and @Walter Roberson's feedback.
J. Alex Lee
J. Alex Lee 2023년 9월 27일
FYI heard back from TMW support confirming that the behavior of
compose("%s%d\\gamma","",4)
is unexpected and is a bug.

댓글을 달려면 로그인하십시오.

답변 (1개)

dpb
dpb 2023년 9월 24일
편집: dpb 2023년 9월 24일
q = compose("%s%d\\gamma/%s",[" ";"-";"-";" "],transpose(1:4),compose("%d",transpose(5:8)))
q = 4×1 string array
" 1\gamma/5" "-2\gamma/6" "-3\gamma/7" " 4\gamma/8"
You are passing an empty string to an expression expecting something in the field.
compose('What does empty%s string do normally','')
ans = 0×0 empty cell array
As you see, that results in an empty output which then turns the string you're trying to generate into an invalid control escape sequence, hence the message.
If the blank space is an issue for some reason, then
q = strtrim(compose("%s%d\\gamma/%s",[" ";"-";"-";" "],transpose(1:4),compose("%d",transpose(5:8))))
q = 4×1 string array
"1\gamma/5" "-2\gamma/6" "-3\gamma/7" "4\gamma/8"
You may have had some reason for the above specific syntax beyond just illustration, but
q=compose("%s%d\\gamma/%d",[" ";"-";"-";" "],[1:4].',[5:8].')
q = 4×1 string array
" 1\gamma/5" "-2\gamma/6" "-3\gamma/7" " 4\gamma/8"
eliminates an unnecessary nested call to compose()
Or
S=[1 -1 -1 1];
A=S.*[1:4];
B=[5:8];
q=compose("%d\\gamma/%d",A.',B.')
q = 4×1 string array
"1\gamma/5" "-2\gamma/6" "-3\gamma/7" "4\gamma/8"
to use variables for data and remove from code -- then can change values without modifying code itself.
  댓글 수: 11
dpb
dpb 2023년 9월 27일
I've come around to your way of thinking that there is a bug in the compose implementation; I was initially caught up with the error message of invalid control field but neglecting that if nothing were prepended to the format string as given it would still be a valid control string and so something internal that is trying to interpret the generated string is/has gone south with the empty character.
Undoubtedly there was never a case in the test suite that had such a characteristic associated with it... :)
Dyuman Joshi
Dyuman Joshi 2023년 10월 4일
@Stephen23, indeed it is and it was your example as well!
Thank you for linking it.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by