how to make string vector?

6 ビュー (過去 30 日間)
Mr M.
Mr M. 2015 年 3 月 31 日
編集済み: Stephen23 2015 年 3 月 31 日
range = [18,29];
how to make ['18','29']?
int2str(range); is not the solution
  1 件のコメント
Stephen23
Stephen23 2015 年 3 月 31 日
編集済み: Stephen23 2015 年 3 月 31 日
Contrary to what many beginners think, in MATLAB the square brackets [] are not a list operator, but are a concatenation operator, which means that the given code
['18','29']
will produce one string like this:
'1829'
A cell array can be used for storing strings separately:
{'18','29'}

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2015 年 3 月 31 日
編集済み: Stephen23 2015 年 3 月 31 日
Here are three possible interpretation of the question.
1. To create one single string, exactly as per the original question:
>> vec = [18,29];
>> sprintf('%d',vec)
ans =
'1829'
2. To create a cell array of strings:
>> arrayfun(@int2str,vec,'UniformOutput',false)
ans =
'18' '29'
3. To create a character array with spaces separating the values:
>> int2str(vec) % or num2str
ans = '18 29'

その他の回答 (1 件)

Jos (10584)
Jos (10584) 2015 年 3 月 31 日
You realise that, in ML, ['18','29'] is exactly the same as the single character array '1829' ?
a = [18, 29]
str = sprintf('%d',a)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by