Question about sprintf not handling list{:} properly
이전 댓글 표시
Hi,
I have a few weird bugs trying to use sprintf on arrays. I've made a toy example of each. Can y'all see what I'm doing wrong? I'm so confused.
1)
X = ["a","b","c"]
Y = [1, 2, 3]
catinated = [X;Y]
a = sprintf("(%s,%d) \n",catinated{:})
When I run this, I get:
>> a =
"(a,49)
(b,50)
(c,51)
"
Y is an array of doubles, so %d should be fine... why is it 49 50 51 instead of 1 2 3?
2) Even weirder - sometimes I this will get rid of the first letter of my string:
X = ["abcd","efgh","i j k l", "m_n_o_p"];
Y = [12.34,56.78,91.01,23.45];
cat = [X;Y];
a = sprintf("%s, %d ", cat{:})
returns
a =
"abcd, 49
2.34, 101
fgh, 53
6.78, 105
j k l, 57
1.01, 109
_n_o_p, 50
3.45, "
So... these numbers are weird and (i think) mixing the askii of characters and cutting the first character from the next line.... and the very last string is just dropped? IDK I'm pretty new to matlab.
For reference,
cat =
2×4 string array
"abcd" "efgh" "i j k l" "m_n_o_p"
"12.34" "56.78" "91.01" "23.45"
At the end of the day, I really wanted an array of each of these combinations as strings, but i found this while trying to do that. Imagine if i wanted my output to be a list like [" A 1", "B 2", "C 3"] - would I have to build it in a for loop or is there a nice way to do this by mashing the string and int array together?
Thanks!
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!