how to assign multiple strings to single variable

조회 수: 8 (최근 30일)
Daniyal Arif
Daniyal Arif 2019년 8월 4일
편집: per isakson 2019년 8월 5일
i have to write this code through loop ..
jan=[jan_1958,jan_1959,jan_1960,jan_1961,jan_1962,jan_1963,jan_1964,jan_1965,...
jan_1966,jan_1967,jan_1968,jan_1969,jan_1970,jan_1971,jan_1972,jan_1973,...
jan_1974,jan_1975,jan_1976,jan_1977,jan_1978,jan_1979,jan_1980,jan_1981,...
jan_1982,jan_1983,jan_1984,jan_1985,jan_1986,jan_1987,jan_1988,jan_1989,...
jan_1990,jan_1991,jan_1992,jan_1993,jan_1994,jan_1995,jan_1996,jan_1997,...
jan_1998,jan_1999,jan_2000,jan_2001,jan_2002,jan_2003,jan_2004,jan_2005,...
jan_2006,jan_2007,jan_2008,jan_2009,jan_2010,jan_2011,jan_2012,jan_2013,...
jan_2014,jan_2015,jan_2016,jan_2017,jan_2018]
i tried a for loop but it gives output
like this
d =
jan_2012
d =
jan_2013
d =
jan_2014
d =
jan_2015
  댓글 수: 7
per isakson
per isakson 2019년 8월 4일
strlength() and string() were introduced in R2016b. Which release do you use?
DELETEKARNAHAI what's that?
Walter Roberson
Walter Roberson 2019년 8월 4일
Either you have a malfunctioning matlab or else you are using an old matlab release and forgot to mention that.. There was exactly one release, R2016b, in which strings existed at all but " could not be used. If you are using any release before that, then it is not possible to assign strings to a variable because strings did not exist then; older releases only had character vectors stored in cell arrays.

댓글을 달려면 로그인하십시오.

채택된 답변

per isakson
per isakson 2019년 8월 4일
편집: per isakson 2019년 8월 4일
Try
>> jan = arrayfun( @(num) sprintf('jan_%4d',num), (1958:2018), 'uni',false );
>> jan(1:3)
ans =
1×3 cell array
{'jan_1958'} {'jan_1959'} {'jan_1960'}
  댓글 수: 2
Guillaume
Guillaume 2019년 8월 4일
If on a release where compose did not exist (i.e pre R2016b), then the undocumented sprintfc could be used:
jan = sprintfc('jan_%d', 1958:2018)
If compose is available:
jan = compose('jan_%d', 1958:2018)
Daniyal Arif
Daniyal Arif 2019년 8월 5일
편집: per isakson 2019년 8월 5일
sir you are great :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by