Escape characters in compose with array input and minus sign *edit* with "empty" string ""
Afficher commentaires plus anciens
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 commentaires
It appears to be about how compose internally parses the string owing to being vectorized, one might presume.
You could submit a support request for an interpretation; doubt anybody every really thought of trying to use it purposefully with an empty string and expecting all permutations thereof to work transparently; an empty object isn't the same thing as a zero-length string that you seem to think should be the result; now whether that would have been a better design choice is possibly open for debate.
Not knowing the actual end use it's hard to conjecture how might solve the problem myself, but from what you've shown us I don't see why either the strtrim() or use of '%d' format string and signed integers aren't useable implementations that both build precisely the string result you're example code is wanting.
J. Alex Lee
le 24 Sep 2023
It's not that "" is empty, it's that compose() of empty is...
I'd venture it's a rare event for a user to deliberately pass a zero-length string or char to compose, so I expect the likelihood of others spending such a time over the particular problem is also approaching zero. :)
I still think it's worthy of an interpretation from TMW as to what would be expected for the use case--it may be a corner case they never thought of or it may be determined to be undesired behavior or (probably most likely) they could add a note to the doc explaining the behavior.
One of the issues I've always had with TMW doc as thorough as it is is that it is descriptive, not normative; there is no single design document that defines the language unequivocally as with Fortran or C Standard so if they don't think to write something down, it isn't possible as you note to determine what expected behavior should be.
Standard languages have an interpretation process for what the Standard interpretation of any questionable case might be; best you can do with MATLAB is make a support request if a staff member doesn't see/respond here.
And oddly,
compose("%s%d\\\\gamma","",4)
compose("%s%d\\gamma",65,4)
compose("%s%d\\gamma",'',4)
compose("%s%d\\gamma"," ",4)
compose("%s %d\\gamma","",4)
compose("%s%d \\gamma","",4)
Clearly there is a bug in processing compose(). For example the version with \\\\ should have had \\ on output.
compose("%s%d\\gamma",'',4)
is the interesting one but strange then that the empty string as input aborts with the error about bad control character...
dpb
le 25 Sep 2023
compose() being relatively new, it's probably not too surprising it still has some warts...you going to submit a bug report?
J. Alex Lee
le 25 Sep 2023
J. Alex Lee
le 27 Sep 2023
Réponses (1)
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 commentaires
J. Alex Lee
le 24 Sep 2023
Modifié(e) : J. Alex Lee
le 24 Sep 2023
Dyuman Joshi
le 24 Sep 2023
There's was a thread about this particular discussion, I can't find it rn (I'll try to find it and like it tomorrow, it's 2 am here rn), but the essence was -
What is the size of
"abcd"
What about
"abc"
then
"ab"
and
"a"
finally
""
The last case for a string object is 0 but compose doesn't return a zero-length string but an empty cellstr object for an empty input.
""
whos ans
compose('%s',"")
strings(0)
string([])
string({})
Some of the ways to get empty string arrays.
As the string class object, yes zero-length is something ("yes, we have no bananas today!") but as @Walter Roberson shows, inside a zero-length string is hidden
s=""
s{:}
the empty char() array. All the string class is is a wrapper around the char() base string array.
Undoubtedly inside the bowels of compose() lies sprintf and the string gets dereferenced internally and boom! I've not delved into the code but the hint from the error and the message that refers to it is just too much to ignore and it is what the base C i/o library uses that almost surely is where it gets to eventually...
There's where the "problem" lies mehinks in expecting that to function in all circumstances...
@Dyuman Joshi: perhaps you were thinking of this thread: https://www.mathworks.com/matlabcentral/answers/378004-why-is-an-empty-string-not-empty-isempty-returns-true-but-isempty-returns-false
J. Alex Lee
le 26 Sep 2023
dpb
le 27 Sep 2023
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
le 4 Oct 2023
@Stephen23, indeed it is and it was your example as well!
Thank you for linking it.
Catégories
En savoir plus sur Data Type Conversion dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!