Escape characters in compose with array input and minus sign *edit* with "empty" string ""
조회 수: 13 (최근 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 = compose("%s%d\\gamma"," ",4) % i understand this works, and I am fine to implement in my code
try
q = compose("%s%d\\gamma","",4) % why doesn't this work?
catch ME
ME
end
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 = compose("%s\\gamma","") % not an issue with "" even if its right next to escape character
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 = compose("%s%s","",compose("%d\\gamma",4)) % this works
q = compose("%s%s\\gamma","4","") % this works, but doesn't matter for my application because its reversed
q = compose("%s%s%s","","4","\gamma") % this works, and is potentially the most intuitive solution that works for me
q = compose("%s%s\\gamma","","4") % this does not work
****** 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
답변 (1개)
dpb
2023년 9월 24일
편집: dpb
2023년 9월 24일
q = compose("%s%d\\gamma/%s",[" ";"-";"-";" "],transpose(1:4),compose("%d",transpose(5:8)))
You are passing an empty string to an expression expecting something in the field.
compose('What does empty%s string do normally','')
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))))
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].')
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.')
to use variables for data and remove from code -- then can change values without modifying code itself.
댓글 수: 11
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
2023년 10월 4일
@Stephen23, indeed it is and it was your example as well!
Thank you for linking it.
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!