HOW to get string variable from vector
4 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
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 件のコメント
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!