필터 지우기
필터 지우기

HOW to get string variable from vector

조회 수: 3 (최근 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 CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by