Borrar filtros
Borrar filtros

HOW to get string variable from vector

3 visualizaciones (últimos 30 días)
bay rem
bay rem el 31 de Dic. de 2015
Respondida: Walter Roberson el 31 de Dic. de 2015
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

Respuesta aceptada

Walter Roberson
Walter Roberson el 31 de Dic. de 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'

Más respuestas (0)

Categorías

Más información sobre Cell Arrays en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by