Convert MMddyyyy from excel, add separate HH:mm from excel into datetime table
Show older comments
Currently I have an excel file that has date in one cell in the format "MMddyyyy" and in a separate cell with a time in the format "HH:mm". I am trying to combine them into a single datetime entry with format "MM/dd/yyyy HH:mm" I can use within appdesigner to fill text fields, and eventually to parse a different file and clean it up to include only specified dates and times with associated data.
This is my code;
datetime.setDefaultFormats('default','MM/dd/yyyy HH:mm');
%load the trial start date and convert to a datetime usable
%format
dateentry = string(readmatrix(strcat(app.directory,'\',app.TrialDataFilename),'Sheet','Data','Range','B2:B2'));
startdate = datetime(dateentry,"InputFormat","MMddyyyy");
timeentry = readmatrix(strcat(app.directory,'\',app.TrialDataFilename),'Sheet','Data','Range','A21:A21');
starttime = datetime(timeentry,"ConvertFrom","datenum");
finalentry = startdate+timeofday(starttime);
%Add to the start time field
app.StartTimemmddyyyyHHMMEditField.Value = string(finalentry);
This just seems totally unreasonable. It does work, but there has to be a more elegant way to do this without creating a ton of variables just for this conversion. I'll have to do this for quite a few date and time entries from an operator log excel sheet and I am concerned about bogging down the app to a crawl.
Accepted Answer
More Answers (0)
Categories
Find more on Dates and Time 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!