How do I change a date in a cell into three cells in MatLab xlsx file?
    2 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
I have an excel file that has a column of dates in an examples such as follows: 01-Oct-2022
How do I change it into three columns, so the day, month, and year can each be in their own cell? Like follows: 1     10     2022
I would like to do this in MatLab as opposed to Excel.
2 commentaires
  the cyclist
      
      
 le 5 Fév 2023
				Can you upload the Excel file? You can use the paper clip icon in the INSERT section of the toolbar.
Excel is notoriously terrible at storing dates, so it is best to use your actual file, rather than guess at the multiple possible ways it could be stored.
Réponse acceptée
  Star Strider
      
      
 le 5 Fév 2023
        
      Modifié(e) : Star Strider
      
      
 le 5 Fév 2023
  
      T1 = table(datetime('now') + days(0:4).', rand(5,1), rand(5,1), 'VariableNames',{'DateTime','Data_1','Data_2'})
[Year,Month,Day] = ymd(T1.DateTime);
T1 = addvars(T1,Day,Month,Year, 'After',1)
EDIT — (5 Feb 2023 at 23:07)
With the provided file — 
WaterData = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1285390/Water%20Data.xlsx', 'VariableNamingRule','preserve')
[Year,Month,Day] = ymd(WaterData.Date);
WaterData = addvars(WaterData,Day,Month,Year, 'After',3)                % Option 1: Keep 'Date' (Recommended)
WaterDAta = removevars(WaterData, 'Date')                               % Option 2: Remove 'Date'
Use either ‘Option 1’,or ‘Option 2’ depending on the desired result.  
.
0 commentaires
Plus de réponses (0)
Voir également
Catégories
				En savoir plus sur Spreadsheets dans Help Center et File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


