# MATLAB fixing dates that are flipped

1 view (last 30 days)
BA on 24 Jul 2022
Commented: Star Strider on 26 Jul 2022
Hey, so I have a dataset that has a few years that are backwards for a few columns. For example,
35, 37 and 49 are 0202 when the year should be 2020.
If someone knows a fix for this to apply to my dataset, that would be great.
The code I have currently works only to flip every year in the column and it only works if the dates are chars. I need the code to work for only a select few years and to work for cells.
% Example
dates = {'3/11/9102'; '3/12/9102'; '3/13/9102'; '3/14/9102'; '3/15/9102'};
% Split by '/', apply fliplr function to '9102' and concatenate
c = split(dates, '/');
c(:, 3) = cellfun(@fliplr, c(:,3), 'UniformOutput', false);
datesNew = join(c, '/');
% Show the result
disp(datesNew)
I have attached the dataset to this post. Thanks in advance
BA on 25 Jul 2022
Whoever closed my question, my apologies but this isn't the same question. Its building off all the previous posts I have made.
Kindly leave this question open. I have been working on this for 3 days and I haven't been able to find a solution

Star Strider on 26 Jul 2022
The first variable is imported as a cell character array, so it has to be changed to a datetime array for the rest of this to work —
T1 = 1333×153 table
VN = T1.Properties.VariableNames; % Cell Array Of Variable Names
dt = datetime(T1{:,1}, 'InputFormat','dd-MMM-yyyy'); % Change 'char' Array To 'datetime'
T11u = unique(year(dt)) % Determine Problem
T11u = 4×1
20 21 2020 2021
Lv = year(dt) < 2020; % Logical Vector
[y,m,d] = ymd(dt(Lv)); % Get Components
dt(Lv) = datetime(y+2000,m,d); % Add 2000 Where Appropriate
T1 = removevars(T1,VN(1)); % Change 'T1' To Accommodate New 'datetime' Variable
T1 = addvars(T1,dt,'Before',VN(2)); % Change 'T1' To Accommodate New 'datetime' Variable
T1.Properties.VariableNames(1) = VN(1) % Change 'T1' To Accommodate New 'datetime' Variable
T1 = 1333×153 table
T11u = unique(year(T1{:,1})) % Check Result
T11u = 2×1
2020 2021
I assume that the ‘20’ years are supposed to be ‘2020’ and the ‘21’ years ‘2021’. I wish this was more straightforward. (I tried a number of different approaches using various datetime optons before I settled on this one, simply because it works.) There may be easier ways to do this, however they are not obvious.
I hope this sort of approach is applicable to similar problems.
.
BA on 26 Jul 2022
Thank you!!!
Star Strider on 26 Jul 2022
As always, my pleasure!

KSSV on 25 Jul 2022
t = datetime(T.(1))
t = 1333×1 datetime array
19-May-2021 20-May-2021 21-May-2021 22-May-2021 23-May-2021 24-May-2021 25-May-2021 26-May-2021 27-May-2021 28-May-2021 29-May-2021 30-May-2021 31-May-2021 01-Jun-2021 02-Jun-2021 03-Jun-2021 04-Jun-2021 05-Jun-2021 06-Jun-2021 07-Jun-2021 08-Jun-2021 09-Jun-2021 10-Jun-2021 11-Jun-2021 12-Jun-2021 13-Jun-2021 14-Jun-2021 15-Jun-2021 29-Sep-2020 30-Sep-2020
##### 3 CommentsShow 1 older commentHide 1 older comment
KSSV on 25 Jul 2022
Where it din't work? Why?
BA on 25 Jul 2022
The dates shown were already 2021. It's just a few dates that are 1202 so that's why it isn't showing in the prompt you ran.
For example
x = ['2/12/1902']
x = ['2/12/1902'];
y = ['6/13/2019'];
datetime(x)
Warning: Successfully converted the text to datetime using the format 'MM/dd/uuuu', but the format is ambiguous and could also be 'dd/MM/uuuu'. To create datetimes from text with a specific format call:

datetime(textinput,'InputFormat',infmt)
ans = datetime
12-Feb-1902
datetime(y)
ans = datetime
13-Jun-2019

### Categories

Find more on File Operations in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!