Combine values of a variable to make new variable

조회 수: 2 (최근 30일)
C G
C G 2018년 4월 7일
답변: Gayatri Menon 2018년 4월 10일
I think this is something that Matlab should be able to do, but for some reason I cannot find anything that will allow me to do this.
What I need is to combine the values in three variables to make a new variable. For example, y=2012, m=10, d=10. I want a new variable dttm=20121010 or ymd, with the format as in YYMMDD, so single digits need to have a leading zero. The variable will be part of a filename for a separate program that requires all digits to be 2 digits, hence the redefining of y to YY.
This is what I have so far and obviously, it isn't working. The loop works fine. Please help. Thank you in advance.
[y, m, d,] = datevec(startdaytime,'yyyymmddHH');
if y>=2000
YY = (y-2000);%('%02.0f');
else y<=1999;
YY = (y-1900);
end
dttm = (YY+m+d,'%02.0f %02.0f %02.0f')
P.S. Before you ask, yes I know Matlab doesn't like leading zeros. Just go with it.
  댓글 수: 1
C G
C G 2018년 4월 7일
If you have noticed, I have asked several questions in the past few weeks. I am in the middle of creating a new Matlab function by translating IDL code into Matlab code. Hence, some of the more random questions.
If anyone is interested in helping me translate many pages of IDL code into Matlab code, please let me know.

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

채택된 답변

Gayatri Menon
Gayatri Menon 2018년 4월 10일
Hi,
Hope the below code helps.
DateString = '28.03.2005';
formatIn = 'dd.mm.yyyy';
[y, m, d]=datevec(DateString,formatIn)
if y>=2000
YY = (y-2000);%('%02.0f');
else y<=1999;
YY = (y-1900);
end
m_new = sprintf('%02d', m);
d_new = sprintf('%02d', d);
YY_new = sprintf('%02d', YY);
out=strcat(YY_new,m_new,d_new);
Thanks

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by