HOW to get string variable from vector

4 ビュー (過去 30 日間)
bay rem
bay rem 2015 年 12 月 31 日
回答済み: Walter Roberson 2015 年 12 月 31 日
hello i've a vector of strings V=['hiver' 'ete' 'automne' 'printemps'] and i wanna get 'hiver' from that vector, i tried V(1) but it gives me the first alphabet 'h'
thank you

採用された回答

Walter Roberson
Walter Roberson 2015 年 12 月 31 日
V=['hiver' 'ete' 'automne' 'printemps']
creates
V = 'hivereteautomneprintemps';
The [] operator is equivalent to horzcat() in this context, as if you had used
V = horzcat('hiver', 'ete', 'automne', 'printemps');
In MATLAB, strings are vectors of characters, so what you did was similar to
V = [[1 2 3 4 5] [6 7 8]]
which is the same as
V = horzcat([1 2 3 4 5], [6 7 8])
which is [1 2 3 4 5 6 7 8]
What you probably wanted to do was
V = {'hiver' 'ete' 'automne' 'printemps'}
{} is used for cell arrays, which are arrays in which each element might be a different size or even a different data type.
V(1) would then be {'hiver'} -- which would still be a cell array. To get the "inside" of the cell array element, use V{1} which would be 'hiver'

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeElementary Math についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by