How to convert date by changine date format from text file?

조회 수: 5 (최근 30일)
Dogan Deniz Karadeniz
Dogan Deniz Karadeniz 2019년 6월 25일
편집: Kojiro Saito 2019년 6월 26일
I'd like to convert date according to first three column as day-month-year hour:minute:second.
  • A column: Year
  • B column: Day of the year
  • C column: Hour
I tried to do it, not achieving.

채택된 답변

Kojiro Saito
Kojiro Saito 2019년 6월 25일
편집: Kojiro Saito 2019년 6월 26일
Simply, you can utilize table's functioinalities.
t = readtable('test5.txt');
% Create datetime and display as "day-month-year hour:minute:second" format
dt = datetime([t.Var1 ones(height(t), 1) t.Var2 t.Var3 zeros(height(t), 1) zeros(height(t), 1)], 'Format', 'dd-MM-yyyy HH:mm:ss');
% Insert datetime variable to the table as "datestr" column
t.datestr = dt;
% Write a table as a file
writetable(t, 'output.txt')
UPDATED
Based on your comment, the following is what you want. removevars and movevars were introduced in R2018a.
t = readtable('test5.txt');
% Convert to datetime
dt = datetime([t.Var1 ones(height(t), 1) t.Var2 t.Var3 zeros(height(t), 1) zeros(height(t), 1)]);
% Insert datetime variables to original table
t.datestr = datetime(dt, 'Format', 'dd-MM-yyyy');
t.timestr = datetime(dt, 'Format', 'HH:mm:ss');
% Delete column 1 to 3
T2 = removevars(t, 1:3);
% Move datestr and timestr to the first and second columns
T2 = movevars(T2, {'datestr', 'timestr'}, 'Before',1);
% Write table as a tab separated file without headers
writetable(T2, 'output.txt', 'WriteVariableNames', false, 'Delimiter', 'tab')
  댓글 수: 2
Dogan Deniz Karadeniz
Dogan Deniz Karadeniz 2019년 6월 25일
I think I didn't explain myself clearly.
Output should be as follow
29-06-2012 00:00:00 30 25 21.2
Kojiro Saito
Kojiro Saito 2019년 6월 25일
I've updated the answer.

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

추가 답변 (1개)

Himanshu Rai
Himanshu Rai 2019년 6월 25일
Try this function, it accepts three vectors corresponding to year, day and hour
function Dt = conv(Year, Day, Hour)
Dt = datetime(Year, 1, 1) + Day - 1 + hours(Hour);
end
  댓글 수: 6
Himanshu Rai
Himanshu Rai 2019년 6월 25일
If you want to read data from the file, use this link - textscan. However note that this was not what was specified in your question. If you want something else please specify it clearly in your question.
Dogan Deniz Karadeniz
Dogan Deniz Karadeniz 2019년 6월 25일
Sorry about that. Indeed, I need to write output other columns together in the text file.

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

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by