Store Variable name as Sequence 't1', 't2', 't3',.......

조회 수: 1 (최근 30일)
Cinko
Cinko 2018년 5월 8일
편집: Guillaume 2018년 5월 8일
I have a problem naming my VariableNames in my array2table.
years={'t1','t2','t3','t4','t5','t6','t7','t8','t9','t10','t11','t12','t13','t14','t15','t16','t17','t18','t19','t20'};
I = array2table(A,'VariableNames', years)
Is there any other way to make a sequence for years? My end date t20 can vary and I don't want to change the variablename each time I want to change my end date.
  댓글 수: 8
Dennis
Dennis 2018년 5월 8일
yes, sorry if i wasn't clear.
Cinko
Cinko 2018년 5월 8일
@Dennis That works great. Thank you.

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

답변 (1개)

Guillaume
Guillaume 2018년 5월 8일
years = compose('t%d', 1:20);
  댓글 수: 3
Guillaume
Guillaume 2018년 5월 8일
편집: Guillaume 2018년 5월 8일
compose requires R2016b or later. You must be on an earlier version. You can use sprintfc instead but be aware that this function is undocumented
years = sprintfc('t%d', 1:20); %note that sprintfc is undocumented. In this case, it behaves like compose introduced in R2016b
If using undocumented functions is not your thing, then
years = arrayfun(@(n) sprintf('t%d', n), 1:20, 'UniformOutput', false);
Cinko
Cinko 2018년 5월 8일
okay thank you

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

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by