MATLAB fixing dates that are flipped

1 次查看(过去 30 天)
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
  1 个评论
BA
BA 2022-7-25
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
Star Strider 2022-7-26
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 —
% opts = detectImportOptions('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1075635/Stacked.xlsx')
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1075635/Stacked.xlsx')
T1 = 1333×153 table
General_Prompt_SurveySubmittedDate_1 General_Prompt_SurveySubmittedTime_1 HowMuchDoYouFeel_Sad_Unhappy_DownInTheDumps_Miserable__1 HowMuchDoYouFeel_InAGoodMood_1 HowMuchDoYouFeel_ConnectedWithOthers_1 HowMuchDoYouFeel_Psychological_emotionalPain_EmotionsT_1 HowMuchDoYouFeel_Bored_1 HowMuchDoYouFeel_LikeYouNeedToEscape_ThingsAreSoBadYou_1 HowMuchDoYouFeel_Agitated_irritated_YouAreCrawlingOutO_1 HowMuchDoYouFeel_Worried_YouAreThinkingAboutBadThingsT_1 HowMuchDoYouFeel_Anxious_FeelingLikeBadThingsCouldHapp_1 HowMuchDoYouFeel_Energetic_YouFeelAwakeAndWithEnergyTo_1 HowMuchDoYouFeel_Self_hatred_YouHateHowYouAreAsAPerson_1 HowMuchDoYouFeel_Angry_YouFeelAnnoyed_Resentment_OrExa_1 HowMuchDoYouFeel_OnEdge_On_highAlert_Or_onGuard___1 HowMuchDoYouFeel_Numb_YouHaveDifficultyFeelingEmotions_1 HowMuchDoYouFeel_Grateful_ThankfulOrAppreciativeForThe_1 HowMuchDoYouFeel_PhysicalPainOrDiscomfort_1 HowMuchDoYouFeel_Hopeless_WhenThingsAreBadYouFeelLikeT_1 HowMuchDoYouFeel_Lonely_LikeYouWantMoreSocialConnectio_1 HowMuchDoYouFeel_Humiliated_YouFeelAshamedOrFoolishInT_1 HowMuchDoYouFeel_DesireToAvoid_YouDesireToAvoidPeople__1 HowMuchDoYouFeel_UrgeToUseAlcohol_1 HowMuchDoYouFeel_UrgeToUseHeroin_1 HowMuchDoYouFeel_UrgeToUseCocaine_1 HowMuchDoYouFeel_UrgeToEatSomethingSweet_1 HowMuchDoYouFeel_UrgeToEatSomethingSavory_1 HowMuchDoYouFeel_UrgeToHurtYourself_UrgeToHurtYourBody_1 HowMuchDoYouFeel_UrgeToDie_1 HowMuchDoYouFeel_UrgeToDoSomethingRiskyOrImpulsive_1 HowMuchDoYouFeel_NeedToBeWithFriendsOrFamily_1 IWouldBeAbleToEnjoyThingsThatIUsuallyLike_favoriteMeal_1 ThePastHourDidAnyOfTheFollowingHappen_ISawHeroin_1 ThePastHourDidAnyOfTheFollowingHappen_SomeoneOfferedMeHeroin_1 ThePastHourDidAnyOfTheFollowingHappen_IUsedHeroin_1 wManyBagsDidYouUse__1 ThePastHourDidYouUseAnyOfTheFollowing__1 wMuchMoney_sWorthOfCocaineDidYouHave__1 wManyDrinksDidYouHave__OneDrinkIsACanOfBeer_AGlassOfWine_ASho_1 wManyBenzosDidYouUse__1 wManyJointsDidYouHave__1 atDidYouUse__1 ThePastHourDidAnyOfTheFollowingHappen_IWantedToSeeWhatWouldHa_1 ThePastHourDidAnyOfTheFollowingHappen_OutOfTheBlueIFeltTempte_1 ThePastHourDidAnyOfTheFollowingHappen_IHandled_10OrMoreInCash_1 ereAreYouRightNow__1 ghtBeforeYouStartedThisSurvey_WhoWereYouWith__1 ooseBelowWhoYouWereWith_CheckAllThatApply__1 wCloseOrConnectedDoYouFeelWithThesePeopleRightNow__1 veYouEverUseUsedDrugsWithAtLeastOneOfThesePeopleBefore__1 ghtBeforeYouStartedThisSurvey_WhatWereYouDoing_CheckAllThatAp_1 General_Prompt_SurveySubmittedDate_2 General_Prompt_SurveySubmittedTime_2 HowMuchDoYouFeel_Sad_Unhappy_DownInTheDumps_Miserable__2 HowMuchDoYouFeel_InAGoodMood_2 HowMuchDoYouFeel_ConnectedWithOthers_2 HowMuchDoYouFeel_Psychological_emotionalPain_EmotionsT_2 HowMuchDoYouFeel_Bored_2 HowMuchDoYouFeel_LikeYouNeedToEscape_ThingsAreSoBadYou_2 HowMuchDoYouFeel_Agitated_irritated_YouAreCrawlingOutO_2 HowMuchDoYouFeel_Worried_YouAreThinkingAboutBadThingsT_2 HowMuchDoYouFeel_Anxious_FeelingLikeBadThingsCouldHapp_2 HowMuchDoYouFeel_Energetic_YouFeelAwakeAndWithEnergyTo_2 HowMuchDoYouFeel_Self_hatred_YouHateHowYouAreAsAPerson_2 HowMuchDoYouFeel_Angry_YouFeelAnnoyed_Resentment_OrExa_2 HowMuchDoYouFeel_OnEdge_On_highAlert_Or_onGuard___2 HowMuchDoYouFeel_Numb_YouHaveDifficultyFeelingEmotions_2 HowMuchDoYouFeel_Grateful_ThankfulOrAppreciativeForThe_2 HowMuchDoYouFeel_PhysicalPainOrDiscomfort_2 HowMuchDoYouFeel_Hopeless_WhenThingsAreBadYouFeelLikeT_2 HowMuchDoYouFeel_Lonely_LikeYouWantMoreSocialConnectio_2 HowMuchDoYouFeel_Humiliated_YouFeelAshamedOrFoolishInT_2 HowMuchDoYouFeel_DesireToAvoid_YouDesireToAvoidPeople__2 HowMuchDoYouFeel_UrgeToUseAlcohol_2 HowMuchDoYouFeel_UrgeToUseHeroin_2 HowMuchDoYouFeel_UrgeToUseCocaine_2 HowMuchDoYouFeel_UrgeToEatSomethingSweet_2 HowMuchDoYouFeel_UrgeToEatSomethingSavory_2 HowMuchDoYouFeel_UrgeToHurtYourself_UrgeToHurtYourBody_2 HowMuchDoYouFeel_UrgeToDie_2 HowMuchDoYouFeel_UrgeToDoSomethingRiskyOrImpulsive_2 HowMuchDoYouFeel_NeedToBeWithFriendsOrFamily_2 IWouldBeAbleToEnjoyThingsThatIUsuallyLike_favoriteMeal_2 ThePastHourDidAnyOfTheFollowingHappen_ISawHeroin_2 ThePastHourDidAnyOfTheFollowingHappen_SomeoneOfferedMeHeroin_2 ThePastHourDidAnyOfTheFollowingHappen_IUsedHeroin_2 wManyBagsDidYouUse__2 ThePastHourDidYouUseAnyOfTheFollowing__2 wMuchMoney_sWorthOfCocaineDidYouHave__2 wManyDrinksDidYouHave__OneDrinkIsACanOfBeer_AGlassOfWine_ASho_2 wManyBenzosDidYouUse__2 wManyJointsDidYouHave__2 atDidYouUse__2 ThePastHourDidAnyOfTheFollowingHappen_IWantedToSeeWhatWouldHa_2 ThePastHourDidAnyOfTheFollowingHappen_OutOfTheBlueIFeltTempte_2 ThePastHourDidAnyOfTheFollowingHappen_IHandled_10OrMoreInCash_2 ereAreYouRightNow__2 ghtBeforeYouStartedThisSurvey_WhoWereYouWith__2 ooseBelowWhoYouWereWith_CheckAllThatApply__2 wCloseOrConnectedDoYouFeelWithThesePeopleRightNow__2 veYouEverUseUsedDrugsWithAtLeastOneOfThesePeopleBefore__2 ghtBeforeYouStartedThisSurvey_WhatWereYouDoing_CheckAllThatAp_2 General_Prompt_SurveySubmittedDate_3 General_Prompt_SurveySubmittedTime_3 HowMuchDoYouFeel_Sad_Unhappy_DownInTheDumps_Miserable__3 HowMuchDoYouFeel_InAGoodMood_3 HowMuchDoYouFeel_ConnectedWithOthers_3 HowMuchDoYouFeel_Psychological_emotionalPain_EmotionsT_3 HowMuchDoYouFeel_Bored_3 HowMuchDoYouFeel_LikeYouNeedToEscape_ThingsAreSoBadYou_3 HowMuchDoYouFeel_Agitated_irritated_YouAreCrawlingOutO_3 HowMuchDoYouFeel_Worried_YouAreThinkingAboutBadThingsT_3 HowMuchDoYouFeel_Anxious_FeelingLikeBadThingsCouldHapp_3 HowMuchDoYouFeel_Energetic_YouFeelAwakeAndWithEnergyTo_3 HowMuchDoYouFeel_Self_hatred_YouHateHowYouAreAsAPerson_3 HowMuchDoYouFeel_Angry_YouFeelAnnoyed_Resentment_OrExa_3 HowMuchDoYouFeel_OnEdge_On_highAlert_Or_onGuard___3 HowMuchDoYouFeel_Numb_YouHaveDifficultyFeelingEmotions_3 HowMuchDoYouFeel_Grateful_ThankfulOrAppreciativeForThe_3 HowMuchDoYouFeel_PhysicalPainOrDiscomfort_3 HowMuchDoYouFeel_Hopeless_WhenThingsAreBadYouFeelLikeT_3 HowMuchDoYouFeel_Lonely_LikeYouWantMoreSocialConnectio_3 HowMuchDoYouFeel_Humiliated_YouFeelAshamedOrFoolishInT_3 HowMuchDoYouFeel_DesireToAvoid_YouDesireToAvoidPeople__3 HowMuchDoYouFeel_UrgeToUseAlcohol_3 HowMuchDoYouFeel_UrgeToUseHeroin_3 HowMuchDoYouFeel_UrgeToUseCocaine_3 HowMuchDoYouFeel_UrgeToEatSomethingSweet_3 HowMuchDoYouFeel_UrgeToEatSomethingSavory_3 HowMuchDoYouFeel_UrgeToHurtYourself_UrgeToHurtYourBody_3 HowMuchDoYouFeel_UrgeToDie_3 HowMuchDoYouFeel_UrgeToDoSomethingRiskyOrImpulsive_3 HowMuchDoYouFeel_NeedToBeWithFriendsOrFamily_3 IWouldBeAbleToEnjoyThingsThatIUsuallyLike_favoriteMeal_3 ThePastHourDidAnyOfTheFollowingHappen_ISawHeroin_3 ThePastHourDidAnyOfTheFollowingHappen_SomeoneOfferedMeHeroin_3 ThePastHourDidAnyOfTheFollowingHappen_IUsedHeroin_3 wManyBagsDidYouUse__3 ThePastHourDidYouUseAnyOfTheFollowing__3 wMuchMoney_sWorthOfCocaineDidYouHave__3 wManyDrinksDidYouHave__OneDrinkIsACanOfBeer_AGlassOfWine_ASho_3 wManyBenzosDidYouUse__3 wManyJointsDidYouHave__3 atDidYouUse__3 ThePastHourDidAnyOfTheFollowingHappen_IWantedToSeeWhatWouldHa_3 ThePastHourDidAnyOfTheFollowingHappen_OutOfTheBlueIFeltTempte_3 ThePastHourDidAnyOfTheFollowingHappen_IHandled_10OrMoreInCash_3 ereAreYouRightNow__3 ghtBeforeYouStartedThisSurvey_WhoWereYouWith__3 ooseBelowWhoYouWereWith_CheckAllThatApply__3 wCloseOrConnectedDoYouFeelWithThesePeopleRightNow__3 veYouEverUseUsedDrugsWithAtLeastOneOfThesePeopleBefore__3 ghtBeforeYouStartedThisSurvey_WhatWereYouDoing_CheckAllThatAp_3 ____________________________________ ____________________________________ ________________________________________________________ ______________________________ ______________________________________ ________________________________________________________ ________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ___________________________________________________ ________________________________________________________ ________________________________________________________ ___________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ___________________________________ __________________________________ ___________________________________ __________________________________________ ___________________________________________ ________________________________________________________ ____________________________ ____________________________________________________ ______________________________________________ ________________________________________________________ __________________________________________________ ______________________________________________________________ ___________________________________________________ _____________________ ________________________________________ _______________________________________ _______________________________________________________________ _______________________ ________________________ _____________________ _______________________________________________________________ _______________________________________________________________ _______________________________________________________________ ____________________________________ _______________________________________________ _______________________________________________________________ ____________________________________________________ _________________________________________________________ ___________________________________________________________________________________________________________________________ ____________________________________ ____________________________________ ________________________________________________________ ______________________________ ______________________________________ ________________________________________________________ ________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ___________________________________________________ ________________________________________________________ ________________________________________________________ ___________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ___________________________________ __________________________________ ___________________________________ __________________________________________ ___________________________________________ ________________________________________________________ ____________________________ ____________________________________________________ ______________________________________________ ________________________________________________________ __________________________________________________ ______________________________________________________________ ___________________________________________________ _____________________ ________________________________________ _______________________________________ _______________________________________________________________ _______________________ ________________________ _____________________ _______________________________________________________________ _______________________________________________________________ _______________________________________________________________ ____________________________________ _______________________________________________ _______________________________________________________________ ____________________________________________________ _________________________________________________________ ___________________________________________________________________________________________________________________________ ____________________________________ ____________________________________ ________________________________________________________ ______________________________ ______________________________________ ________________________________________________________ ________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ___________________________________________________ ________________________________________________________ ________________________________________________________ ___________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ___________________________________ __________________________________ ___________________________________ __________________________________________ ___________________________________________ ________________________________________________________ ____________________________ ____________________________________________________ ______________________________________________ ________________________________________________________ __________________________________________________ ______________________________________________________________ ___________________________________________________ _____________________ ________________________________________ _______________________________________ _______________________________________________________________ _______________________ ________________________ _____________________ _______________________________________________________________ _______________________________________________________________ _______________________________________________________________ ____________________ _______________________________________________ _______________________________________________________________ ____________________________________________________ _________________________________________________________ ____________________________________________________________________________________________________________________________________________________ {'19-May-2021'} {'12:09:16'} 5 79 53 28 7 29 49 9 8 63 8 8 6 5 53 75 2 4 3 81 2 10 2 27 49 2 3 0 5 51 {'No' } {'No' } {'No' } NaN {'None'} NaN NaN NaN NaN {'CONDITION_SKIPPED'} {'No' } {'No' } {'Yes'} {'At home' } {'With people in person' } {'Children in your family (e.g. your kids, niece, etc.)' } {'94' } NaN {'Watching TV/listening to music/playing video games,Texting, using social media, or browsing the web' } {'19-May-2021'} {'13:19:28'} 8 44 86 53 5 57 57 2 4 55 4 17 10 2 74 4 8 2 5 5 6 2 2 61 77 0 0 2 31 79 {'No' } {'No' } {'No' } NaN {'None' } NaN NaN NaN NaN {'CONDITION_SKIPPED'} {'No' } {'No' } {'Yes' } {'At home' } {'With people online/over the phone' } {'Children in your family (e.g. your kids, niece, etc.)' } {'93' } NaN {'Texting, using social media, or browsing the web' } {'19-May-2021'} {'18:58:10'} 4 77 69 38 8 10 4 3 5 5 67 2 20 8 81 50 3 4 10 57 4 3 2 59 83 1 0 2 34 73 {'No' } {'No' } {'No' } NaN {'None' } NaN NaN NaN NaN {'CONDITION_SKIPPED'} {'No' } {'No' } {'Yes' } {'At home'} {'Alone' } {'Alone,Children in your family (e.g. your kids, niece, etc.)'} {'92' } NaN {'Watching TV/listening to music/playing video games,Texting, using social media, or browsing the web,Video chat (Google Hangout, Skype, FaceTime)'} {'20-May-2021'} {'10:45:02'} 25 50 51 52 8 26 56 52 48 34 12 26 23 34 55 50 20 4 4 86 14 8 3 53 55 7 2 2 6 2 {'Yes'} {'Yes'} {'No' } NaN {'None'} NaN NaN NaN NaN {'CONDITION_SKIPPED'} {'No' } {'No' } {'Yes'} {'At home' } {'With people in person' } {'Children in your family (e.g. your kids, niece, etc.)' } {'85' } NaN {'Texting, using social media, or browsing the web' } {'20-May-2021'} {'11:16:23'} 2 46 66 55 2 5 39 41 6 54 0 11 10 8 81 44 1 2 26 69 6 4 2 53 71 0 2 8 10 55 {'No' } {'No' } {'No' } NaN {'None' } NaN NaN NaN NaN {'CONDITION_SKIPPED'} {'No' } {'No' } {'Yes' } {'At home' } {'Alone' } {'Alone' } {'CONDITION_SKIPPED'} NaN {'Texting, using social media, or browsing the web' } {'20-May-2021'} {'15:49:01'} 4 66 73 51 1 1 4 0 5 4 0 2 0 7 78 10 11 0 13 8 4 5 4 83 74 0 2 0 50 80 {'No' } {'No' } {'No' } NaN {'None' } NaN NaN NaN NaN {'CONDITION_SKIPPED'} {'No' } {'No' } {'Yes' } {'At home'} {'Alone,With people in person'} {'Alone,Children in your family (e.g. your kids, niece, etc.)'} {'90' } NaN {'Texting, using social media, or browsing the web' } {'21-May-2021'} {'17:20:19'} 0 85 69 47 4 4 1 10 5 49 1 3 10 6 81 6 10 6 9 8 2 2 3 48 51 6 2 6 10 42 {'No' } {'No' } {'No' } NaN {'None'} NaN NaN NaN NaN {'CONDITION_SKIPPED'} {'No' } {'No' } {'Yes'} {'At home' } {'With people in person' } {'Alone,Children in your family (e.g. your kids, niece, etc.)'} {'84' } NaN {'Watching TV/listening to music/playing video games,Texting, using social media, or browsing the web' } {'21-May-2021'} {'17:34:24'} 6 91 53 5 6 5 10 33 6 21 8 0 14 8 78 36 9 13 9 55 3 2 0 67 67 0 5 6 13 53 {'No' } {'No' } {'No' } NaN {'None' } NaN NaN NaN NaN {'CONDITION_SKIPPED'} {'No' } {'No' } {'Yes' } {'At home' } {'Alone,With people online/over the phone'} {'Alone,Children in your family (e.g. your kids, niece, etc.)'} {'90' } NaN {'Watching TV/listening to music/playing video games,Resting or sleeping' } {'21-May-2021'} {'19:56:30'} 2 55 73 6 61 23 54 56 33 78 2 5 30 10 65 5 5 5 2 82 7 0 0 51 63 2 0 0 34 49 {'No' } {'No' } {'No' } NaN {'None' } NaN NaN NaN NaN {'CONDITION_SKIPPED'} {'No' } {'No' } {'Yes' } {'At home'} {'With people in person' } {'Children in your family (e.g. your kids, niece, etc.)' } {'83' } NaN {'Watching TV/listening to music/playing video games,Texting, using social media, or browsing the web' } {'22-May-2021'} {'14:11:41'} 3 69 59 5 1 5 0 38 9 60 3 2 5 4 91 46 0 3 3 74 3 3 3 43 64 0 2 0 2 37 {'No' } {'No' } {'No' } NaN {'None'} NaN NaN NaN NaN {'CONDITION_SKIPPED'} {'No' } {'No' } {'Yes'} {'At home' } {'Alone,With people in person' } {'Alone,Children in your family (e.g. your kids, niece, etc.)'} {'94' } NaN {'Watching TV/listening to music/playing video games,Texting, using social media, or browsing the web,Resting or sleeping'} {'22-May-2021'} {'16:56:22'} 0 53 71 14 4 2 5 10 3 60 0 10 5 3 70 2 3 23 8 72 2 6 2 56 65 6 0 0 6 45 {'No' } {'Yes' } {'Yes' } NaN {'None' } NaN NaN NaN NaN {'CONDITION_SKIPPED'} {'Yes' } {'Yes' } {'Yes' } {'At home' } {'Alone' } {'Alone' } {'CONDITION_SKIPPED'} NaN {'Texting, using social media, or browsing the web,Resting or sleeping,Socializing/hanging out' } {'22-May-2021'} {'19:23:20'} 7 71 67 9 6 19 0 21 10 61 1 2 4 3 88 25 2 4 2 68 2 8 5 62 82 2 2 6 5 29 {'Yes' } {'No' } {'Yes' } NaN {'None' } NaN NaN NaN NaN {'CONDITION_SKIPPED'} {'Yes' } {'Yes' } {'Yes' } {'At home'} {'Alone' } {'Alone' } {'CONDITION_SKIPPED'} NaN {'Watching TV/listening to music/playing video games,Resting or sleeping' }
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
General_Prompt_SurveySubmittedDate_1 General_Prompt_SurveySubmittedTime_1 HowMuchDoYouFeel_Sad_Unhappy_DownInTheDumps_Miserable__1 HowMuchDoYouFeel_InAGoodMood_1 HowMuchDoYouFeel_ConnectedWithOthers_1 HowMuchDoYouFeel_Psychological_emotionalPain_EmotionsT_1 HowMuchDoYouFeel_Bored_1 HowMuchDoYouFeel_LikeYouNeedToEscape_ThingsAreSoBadYou_1 HowMuchDoYouFeel_Agitated_irritated_YouAreCrawlingOutO_1 HowMuchDoYouFeel_Worried_YouAreThinkingAboutBadThingsT_1 HowMuchDoYouFeel_Anxious_FeelingLikeBadThingsCouldHapp_1 HowMuchDoYouFeel_Energetic_YouFeelAwakeAndWithEnergyTo_1 HowMuchDoYouFeel_Self_hatred_YouHateHowYouAreAsAPerson_1 HowMuchDoYouFeel_Angry_YouFeelAnnoyed_Resentment_OrExa_1 HowMuchDoYouFeel_OnEdge_On_highAlert_Or_onGuard___1 HowMuchDoYouFeel_Numb_YouHaveDifficultyFeelingEmotions_1 HowMuchDoYouFeel_Grateful_ThankfulOrAppreciativeForThe_1 HowMuchDoYouFeel_PhysicalPainOrDiscomfort_1 HowMuchDoYouFeel_Hopeless_WhenThingsAreBadYouFeelLikeT_1 HowMuchDoYouFeel_Lonely_LikeYouWantMoreSocialConnectio_1 HowMuchDoYouFeel_Humiliated_YouFeelAshamedOrFoolishInT_1 HowMuchDoYouFeel_DesireToAvoid_YouDesireToAvoidPeople__1 HowMuchDoYouFeel_UrgeToUseAlcohol_1 HowMuchDoYouFeel_UrgeToUseHeroin_1 HowMuchDoYouFeel_UrgeToUseCocaine_1 HowMuchDoYouFeel_UrgeToEatSomethingSweet_1 HowMuchDoYouFeel_UrgeToEatSomethingSavory_1 HowMuchDoYouFeel_UrgeToHurtYourself_UrgeToHurtYourBody_1 HowMuchDoYouFeel_UrgeToDie_1 HowMuchDoYouFeel_UrgeToDoSomethingRiskyOrImpulsive_1 HowMuchDoYouFeel_NeedToBeWithFriendsOrFamily_1 IWouldBeAbleToEnjoyThingsThatIUsuallyLike_favoriteMeal_1 ThePastHourDidAnyOfTheFollowingHappen_ISawHeroin_1 ThePastHourDidAnyOfTheFollowingHappen_SomeoneOfferedMeHeroin_1 ThePastHourDidAnyOfTheFollowingHappen_IUsedHeroin_1 wManyBagsDidYouUse__1 ThePastHourDidYouUseAnyOfTheFollowing__1 wMuchMoney_sWorthOfCocaineDidYouHave__1 wManyDrinksDidYouHave__OneDrinkIsACanOfBeer_AGlassOfWine_ASho_1 wManyBenzosDidYouUse__1 wManyJointsDidYouHave__1 atDidYouUse__1 ThePastHourDidAnyOfTheFollowingHappen_IWantedToSeeWhatWouldHa_1 ThePastHourDidAnyOfTheFollowingHappen_OutOfTheBlueIFeltTempte_1 ThePastHourDidAnyOfTheFollowingHappen_IHandled_10OrMoreInCash_1 ereAreYouRightNow__1 ghtBeforeYouStartedThisSurvey_WhoWereYouWith__1 ooseBelowWhoYouWereWith_CheckAllThatApply__1 wCloseOrConnectedDoYouFeelWithThesePeopleRightNow__1 veYouEverUseUsedDrugsWithAtLeastOneOfThesePeopleBefore__1 ghtBeforeYouStartedThisSurvey_WhatWereYouDoing_CheckAllThatAp_1 General_Prompt_SurveySubmittedDate_2 General_Prompt_SurveySubmittedTime_2 HowMuchDoYouFeel_Sad_Unhappy_DownInTheDumps_Miserable__2 HowMuchDoYouFeel_InAGoodMood_2 HowMuchDoYouFeel_ConnectedWithOthers_2 HowMuchDoYouFeel_Psychological_emotionalPain_EmotionsT_2 HowMuchDoYouFeel_Bored_2 HowMuchDoYouFeel_LikeYouNeedToEscape_ThingsAreSoBadYou_2 HowMuchDoYouFeel_Agitated_irritated_YouAreCrawlingOutO_2 HowMuchDoYouFeel_Worried_YouAreThinkingAboutBadThingsT_2 HowMuchDoYouFeel_Anxious_FeelingLikeBadThingsCouldHapp_2 HowMuchDoYouFeel_Energetic_YouFeelAwakeAndWithEnergyTo_2 HowMuchDoYouFeel_Self_hatred_YouHateHowYouAreAsAPerson_2 HowMuchDoYouFeel_Angry_YouFeelAnnoyed_Resentment_OrExa_2 HowMuchDoYouFeel_OnEdge_On_highAlert_Or_onGuard___2 HowMuchDoYouFeel_Numb_YouHaveDifficultyFeelingEmotions_2 HowMuchDoYouFeel_Grateful_ThankfulOrAppreciativeForThe_2 HowMuchDoYouFeel_PhysicalPainOrDiscomfort_2 HowMuchDoYouFeel_Hopeless_WhenThingsAreBadYouFeelLikeT_2 HowMuchDoYouFeel_Lonely_LikeYouWantMoreSocialConnectio_2 HowMuchDoYouFeel_Humiliated_YouFeelAshamedOrFoolishInT_2 HowMuchDoYouFeel_DesireToAvoid_YouDesireToAvoidPeople__2 HowMuchDoYouFeel_UrgeToUseAlcohol_2 HowMuchDoYouFeel_UrgeToUseHeroin_2 HowMuchDoYouFeel_UrgeToUseCocaine_2 HowMuchDoYouFeel_UrgeToEatSomethingSweet_2 HowMuchDoYouFeel_UrgeToEatSomethingSavory_2 HowMuchDoYouFeel_UrgeToHurtYourself_UrgeToHurtYourBody_2 HowMuchDoYouFeel_UrgeToDie_2 HowMuchDoYouFeel_UrgeToDoSomethingRiskyOrImpulsive_2 HowMuchDoYouFeel_NeedToBeWithFriendsOrFamily_2 IWouldBeAbleToEnjoyThingsThatIUsuallyLike_favoriteMeal_2 ThePastHourDidAnyOfTheFollowingHappen_ISawHeroin_2 ThePastHourDidAnyOfTheFollowingHappen_SomeoneOfferedMeHeroin_2 ThePastHourDidAnyOfTheFollowingHappen_IUsedHeroin_2 wManyBagsDidYouUse__2 ThePastHourDidYouUseAnyOfTheFollowing__2 wMuchMoney_sWorthOfCocaineDidYouHave__2 wManyDrinksDidYouHave__OneDrinkIsACanOfBeer_AGlassOfWine_ASho_2 wManyBenzosDidYouUse__2 wManyJointsDidYouHave__2 atDidYouUse__2 ThePastHourDidAnyOfTheFollowingHappen_IWantedToSeeWhatWouldHa_2 ThePastHourDidAnyOfTheFollowingHappen_OutOfTheBlueIFeltTempte_2 ThePastHourDidAnyOfTheFollowingHappen_IHandled_10OrMoreInCash_2 ereAreYouRightNow__2 ghtBeforeYouStartedThisSurvey_WhoWereYouWith__2 ooseBelowWhoYouWereWith_CheckAllThatApply__2 wCloseOrConnectedDoYouFeelWithThesePeopleRightNow__2 veYouEverUseUsedDrugsWithAtLeastOneOfThesePeopleBefore__2 ghtBeforeYouStartedThisSurvey_WhatWereYouDoing_CheckAllThatAp_2 General_Prompt_SurveySubmittedDate_3 General_Prompt_SurveySubmittedTime_3 HowMuchDoYouFeel_Sad_Unhappy_DownInTheDumps_Miserable__3 HowMuchDoYouFeel_InAGoodMood_3 HowMuchDoYouFeel_ConnectedWithOthers_3 HowMuchDoYouFeel_Psychological_emotionalPain_EmotionsT_3 HowMuchDoYouFeel_Bored_3 HowMuchDoYouFeel_LikeYouNeedToEscape_ThingsAreSoBadYou_3 HowMuchDoYouFeel_Agitated_irritated_YouAreCrawlingOutO_3 HowMuchDoYouFeel_Worried_YouAreThinkingAboutBadThingsT_3 HowMuchDoYouFeel_Anxious_FeelingLikeBadThingsCouldHapp_3 HowMuchDoYouFeel_Energetic_YouFeelAwakeAndWithEnergyTo_3 HowMuchDoYouFeel_Self_hatred_YouHateHowYouAreAsAPerson_3 HowMuchDoYouFeel_Angry_YouFeelAnnoyed_Resentment_OrExa_3 HowMuchDoYouFeel_OnEdge_On_highAlert_Or_onGuard___3 HowMuchDoYouFeel_Numb_YouHaveDifficultyFeelingEmotions_3 HowMuchDoYouFeel_Grateful_ThankfulOrAppreciativeForThe_3 HowMuchDoYouFeel_PhysicalPainOrDiscomfort_3 HowMuchDoYouFeel_Hopeless_WhenThingsAreBadYouFeelLikeT_3 HowMuchDoYouFeel_Lonely_LikeYouWantMoreSocialConnectio_3 HowMuchDoYouFeel_Humiliated_YouFeelAshamedOrFoolishInT_3 HowMuchDoYouFeel_DesireToAvoid_YouDesireToAvoidPeople__3 HowMuchDoYouFeel_UrgeToUseAlcohol_3 HowMuchDoYouFeel_UrgeToUseHeroin_3 HowMuchDoYouFeel_UrgeToUseCocaine_3 HowMuchDoYouFeel_UrgeToEatSomethingSweet_3 HowMuchDoYouFeel_UrgeToEatSomethingSavory_3 HowMuchDoYouFeel_UrgeToHurtYourself_UrgeToHurtYourBody_3 HowMuchDoYouFeel_UrgeToDie_3 HowMuchDoYouFeel_UrgeToDoSomethingRiskyOrImpulsive_3 HowMuchDoYouFeel_NeedToBeWithFriendsOrFamily_3 IWouldBeAbleToEnjoyThingsThatIUsuallyLike_favoriteMeal_3 ThePastHourDidAnyOfTheFollowingHappen_ISawHeroin_3 ThePastHourDidAnyOfTheFollowingHappen_SomeoneOfferedMeHeroin_3 ThePastHourDidAnyOfTheFollowingHappen_IUsedHeroin_3 wManyBagsDidYouUse__3 ThePastHourDidYouUseAnyOfTheFollowing__3 wMuchMoney_sWorthOfCocaineDidYouHave__3 wManyDrinksDidYouHave__OneDrinkIsACanOfBeer_AGlassOfWine_ASho_3 wManyBenzosDidYouUse__3 wManyJointsDidYouHave__3 atDidYouUse__3 ThePastHourDidAnyOfTheFollowingHappen_IWantedToSeeWhatWouldHa_3 ThePastHourDidAnyOfTheFollowingHappen_OutOfTheBlueIFeltTempte_3 ThePastHourDidAnyOfTheFollowingHappen_IHandled_10OrMoreInCash_3 ereAreYouRightNow__3 ghtBeforeYouStartedThisSurvey_WhoWereYouWith__3 ooseBelowWhoYouWereWith_CheckAllThatApply__3 wCloseOrConnectedDoYouFeelWithThesePeopleRightNow__3 veYouEverUseUsedDrugsWithAtLeastOneOfThesePeopleBefore__3 ghtBeforeYouStartedThisSurvey_WhatWereYouDoing_CheckAllThatAp_3 ____________________________________ ____________________________________ ________________________________________________________ ______________________________ ______________________________________ ________________________________________________________ ________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ___________________________________________________ ________________________________________________________ ________________________________________________________ ___________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ___________________________________ __________________________________ ___________________________________ __________________________________________ ___________________________________________ ________________________________________________________ ____________________________ ____________________________________________________ ______________________________________________ ________________________________________________________ __________________________________________________ ______________________________________________________________ ___________________________________________________ _____________________ ________________________________________ _______________________________________ _______________________________________________________________ _______________________ ________________________ _____________________ _______________________________________________________________ _______________________________________________________________ _______________________________________________________________ ____________________________________ _______________________________________________ _______________________________________________________________ ____________________________________________________ _________________________________________________________ ___________________________________________________________________________________________________________________________ ____________________________________ ____________________________________ ________________________________________________________ ______________________________ ______________________________________ ________________________________________________________ ________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ___________________________________________________ ________________________________________________________ ________________________________________________________ ___________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ___________________________________ __________________________________ ___________________________________ __________________________________________ ___________________________________________ ________________________________________________________ ____________________________ ____________________________________________________ ______________________________________________ ________________________________________________________ __________________________________________________ ______________________________________________________________ ___________________________________________________ _____________________ ________________________________________ _______________________________________ _______________________________________________________________ _______________________ ________________________ _____________________ _______________________________________________________________ _______________________________________________________________ _______________________________________________________________ ____________________________________ _______________________________________________ _______________________________________________________________ ____________________________________________________ _________________________________________________________ ___________________________________________________________________________________________________________________________ ____________________________________ ____________________________________ ________________________________________________________ ______________________________ ______________________________________ ________________________________________________________ ________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ___________________________________________________ ________________________________________________________ ________________________________________________________ ___________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ________________________________________________________ ___________________________________ __________________________________ ___________________________________ __________________________________________ ___________________________________________ ________________________________________________________ ____________________________ ____________________________________________________ ______________________________________________ ________________________________________________________ __________________________________________________ ______________________________________________________________ ___________________________________________________ _____________________ ________________________________________ _______________________________________ _______________________________________________________________ _______________________ ________________________ _____________________ _______________________________________________________________ _______________________________________________________________ _______________________________________________________________ ____________________ _______________________________________________ _______________________________________________________________ ____________________________________________________ _________________________________________________________ ____________________________________________________________________________________________________________________________________________________ 19-May-2021 {'12:09:16'} 5 79 53 28 7 29 49 9 8 63 8 8 6 5 53 75 2 4 3 81 2 10 2 27 49 2 3 0 5 51 {'No' } {'No' } {'No' } NaN {'None'} NaN NaN NaN NaN {'CONDITION_SKIPPED'} {'No' } {'No' } {'Yes'} {'At home' } {'With people in person' } {'Children in your family (e.g. your kids, niece, etc.)' } {'94' } NaN {'Watching TV/listening to music/playing video games,Texting, using social media, or browsing the web' } {'19-May-2021'} {'13:19:28'} 8 44 86 53 5 57 57 2 4 55 4 17 10 2 74 4 8 2 5 5 6 2 2 61 77 0 0 2 31 79 {'No' } {'No' } {'No' } NaN {'None' } NaN NaN NaN NaN {'CONDITION_SKIPPED'} {'No' } {'No' } {'Yes' } {'At home' } {'With people online/over the phone' } {'Children in your family (e.g. your kids, niece, etc.)' } {'93' } NaN {'Texting, using social media, or browsing the web' } {'19-May-2021'} {'18:58:10'} 4 77 69 38 8 10 4 3 5 5 67 2 20 8 81 50 3 4 10 57 4 3 2 59 83 1 0 2 34 73 {'No' } {'No' } {'No' } NaN {'None' } NaN NaN NaN NaN {'CONDITION_SKIPPED'} {'No' } {'No' } {'Yes' } {'At home'} {'Alone' } {'Alone,Children in your family (e.g. your kids, niece, etc.)'} {'92' } NaN {'Watching TV/listening to music/playing video games,Texting, using social media, or browsing the web,Video chat (Google Hangout, Skype, FaceTime)'} 20-May-2021 {'10:45:02'} 25 50 51 52 8 26 56 52 48 34 12 26 23 34 55 50 20 4 4 86 14 8 3 53 55 7 2 2 6 2 {'Yes'} {'Yes'} {'No' } NaN {'None'} NaN NaN NaN NaN {'CONDITION_SKIPPED'} {'No' } {'No' } {'Yes'} {'At home' } {'With people in person' } {'Children in your family (e.g. your kids, niece, etc.)' } {'85' } NaN {'Texting, using social media, or browsing the web' } {'20-May-2021'} {'11:16:23'} 2 46 66 55 2 5 39 41 6 54 0 11 10 8 81 44 1 2 26 69 6 4 2 53 71 0 2 8 10 55 {'No' } {'No' } {'No' } NaN {'None' } NaN NaN NaN NaN {'CONDITION_SKIPPED'} {'No' } {'No' } {'Yes' } {'At home' } {'Alone' } {'Alone' } {'CONDITION_SKIPPED'} NaN {'Texting, using social media, or browsing the web' } {'20-May-2021'} {'15:49:01'} 4 66 73 51 1 1 4 0 5 4 0 2 0 7 78 10 11 0 13 8 4 5 4 83 74 0 2 0 50 80 {'No' } {'No' } {'No' } NaN {'None' } NaN NaN NaN NaN {'CONDITION_SKIPPED'} {'No' } {'No' } {'Yes' } {'At home'} {'Alone,With people in person'} {'Alone,Children in your family (e.g. your kids, niece, etc.)'} {'90' } NaN {'Texting, using social media, or browsing the web' } 21-May-2021 {'17:20:19'} 0 85 69 47 4 4 1 10 5 49 1 3 10 6 81 6 10 6 9 8 2 2 3 48 51 6 2 6 10 42 {'No' } {'No' } {'No' } NaN {'None'} NaN NaN NaN NaN {'CONDITION_SKIPPED'} {'No' } {'No' } {'Yes'} {'At home' } {'With people in person' } {'Alone,Children in your family (e.g. your kids, niece, etc.)'} {'84' } NaN {'Watching TV/listening to music/playing video games,Texting, using social media, or browsing the web' } {'21-May-2021'} {'17:34:24'} 6 91 53 5 6 5 10 33 6 21 8 0 14 8 78 36 9 13 9 55 3 2 0 67 67 0 5 6 13 53 {'No' } {'No' } {'No' } NaN {'None' } NaN NaN NaN NaN {'CONDITION_SKIPPED'} {'No' } {'No' } {'Yes' } {'At home' } {'Alone,With people online/over the phone'} {'Alone,Children in your family (e.g. your kids, niece, etc.)'} {'90' } NaN {'Watching TV/listening to music/playing video games,Resting or sleeping' } {'21-May-2021'} {'19:56:30'} 2 55 73 6 61 23 54 56 33 78 2 5 30 10 65 5 5 5 2 82 7 0 0 51 63 2 0 0 34 49 {'No' } {'No' } {'No' } NaN {'None' } NaN NaN NaN NaN {'CONDITION_SKIPPED'} {'No' } {'No' } {'Yes' } {'At home'} {'With people in person' } {'Children in your family (e.g. your kids, niece, etc.)' } {'83' } NaN {'Watching TV/listening to music/playing video games,Texting, using social media, or browsing the web' } 22-May-2021 {'14:11:41'} 3 69 59 5 1 5 0 38 9 60 3 2 5 4 91 46 0 3 3 74 3 3 3 43 64 0 2 0 2 37 {'No' } {'No' } {'No' } NaN {'None'} NaN NaN NaN NaN {'CONDITION_SKIPPED'} {'No' } {'No' } {'Yes'} {'At home' } {'Alone,With people in person' } {'Alone,Children in your family (e.g. your kids, niece, etc.)'} {'94' } NaN {'Watching TV/listening to music/playing video games,Texting, using social media, or browsing the web,Resting or sleeping'} {'22-May-2021'} {'16:56:22'} 0 53 71 14 4 2 5 10 3 60 0 10 5 3 70 2 3 23 8 72 2 6 2 56 65 6 0 0 6 45 {'No' } {'Yes' } {'Yes' } NaN {'None' } NaN NaN NaN NaN {'CONDITION_SKIPPED'} {'Yes' } {'Yes' } {'Yes' } {'At home' } {'Alone' } {'Alone' } {'CONDITION_SKIPPED'} NaN {'Texting, using social media, or browsing the web,Resting or sleeping,Socializing/hanging out' } {'22-May-2021'} {'19:23:20'} 7 71 67 9 6 19 0 21 10 61 1 2 4 3 88 25 2 4 2 68 2 8 5 62 82 2 2 6 5 29 {'Yes' } {'No' } {'Yes' } NaN {'None' } NaN NaN NaN NaN {'CONDITION_SKIPPED'} {'Yes' } {'Yes' } {'Yes' } {'At home'} {'Alone' } {'Alone' } {'CONDITION_SKIPPED'} NaN {'Watching TV/listening to music/playing video games,Resting or sleeping' }
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.
.

更多回答(1 个)

KSSV
KSSV 2022-7-25
T = readtable('https://in.mathworks.com/matlabcentral/answers/uploaded_files/1075635/Stacked.xlsx') ;
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 个评论
BA
BA 2022-7-25
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

请先登录,再进行评论。

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by