Azzera filtri
Azzera filtri

HOW to get string variable from vector

3 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Walter Roberson
Walter Roberson il 31 Dic 2015
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'

Più risposte (0)

Categorie

Scopri di più su Cell Arrays in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by