array of strings from an array of integers

55 次查看(过去 30 天)
Hi if I have an array of integers like this:
A=[123 456 789]
How do I get an array of strings, say named B, such that:
BB(1) = 123 (this is string 1) BB(2) = 456 (this is string 2) BB(3) = 789 (this is string 3)
What I am trying to do is use integer values as labels for my xtick labels, and cannot figure out how to do it. Thank you.

采纳的回答

Matt J
Matt J 2013-4-25
编辑:Matt J 2013-4-25
The output would have to be a cell array
>> BB=arrayfun(@(a)num2str(a),A,'uni',0)
BB =
'123' '456' '789'
>> BB{1}, BB{2}
ans =
123
ans =
456

更多回答(2 个)

Steven Lord
Steven Lord 2021-2-12
If you're using a release that supports the string array (which did not exist when the original question was asked) use string.
A = randi(999, 1, 5)
A = 1×5
790 270 251 663 335
plot(1:5, 1:5, 'o-')
xticks(1:5)
xticklabels(string(A))
  1 个评论
Walter Roberson
Walter Roberson 2021-2-12
xticklabels() as a function did not exist when the question was originally asked either ;-)

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2021-2-12
A = randi(999, 1, 5)
A = 1×5
440 755 70 554 220
plot(1:5, 1:5, 'o-')
xticks(1:5)
xticklabels(compose('%d',A)) %needs R2016b or later
yticklabels(sprintfc('%d', A)) %undocumented

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by