How can I create a new column with date info with specific date format ?

10 views (last 30 days)
Hi everyone
I need to make a new column with date info from a file like the one below, but with this format:
20180101.000000
20180101.060000
%year mm dd hh
1997 1 1 0
1997 1 1 6
1997 1 1 12
1997 1 1 18
1997 1 2 0
1997 1 2 6
Thank you for taking the time, any help is appreciated!

Answers (1)

BhaTTa
BhaTTa on 14 Oct 2024
I assume that you have date saved in the format below
%year mm dd hh
1997 1 1 0
and you want to convert it into the format below
20180101.000000
Below I have attached an example code to achieve it
data = [
1997 1 1 0
1997 1 1 6
1997 1 1 12
1997 1 1 18
1997 1 2 0
1997 1 2 6
];
formattedDates = cell(size(data, 1), 1);
for i = 1:size(data, 1)
year = data(i, 1);
month = data(i, 2);
day = data(i, 3);
hour = data(i, 4);
% Format the date and time into the desired string format
formattedDates{i} = sprintf('%04d%02d%02d.%02d0000', year, month, day, hour);
end
disp(formattedDates);
Hope it helps.

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!