MATLAB: How to convert date by changine date format from text file

Dates and Timetext file

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.

Best Answer

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')