MATLAB: How to convert a string to a date convert a string to a date I have a txt file with dates in the following form: 200309302300 200309302310 200309302320 ……etc How can I convert these strings into date form? something like : 2003-Sep-30 23:00 or 2003-09-30 23:00 Best Answer I am not certain if the numbers are themselves strings, or numbers. If they are numbers, this works: numdate = [200309302300 200309302310 200309302320];dn = datenum(num2str(numdate), 'yyyymmddHHMM') % Date Numbersdv = datevec(dn)dt = datetime(num2str(numdate), 'InputFormat','yyyyMMddHHmm') % ‘datetime’ ArrayIf they are strings, omit the str2num calls and just insert them directly. Related SolutionsMATLAB: Convert day year hour data series with missing data to a serial number f = fopen('dates1.txt');c = textscan(f,'%s %s','collectoutput',1);fclose(f);out = datenum(strcat(c{1}(:,1),{'_'},c{1}(:,2)),'dd/mm/yyyy_HH:MM');orf = fopen('dates1.txt');c = textscan(f,'%s','delimiter','\n');fclose(f);out = datenum(c{1},'dd/mm/yyyy HH:MM') MATLAB: Conversion of dates to a different format The new date and time functions probably have built-in functions to do what you want, but in their absence, this works:t_str = '20110530';dn = datenum('20110530', 'yyyymmdd');dnstart = datenum(dt_str(1:4), 'yyyy'); % Start Of 2011dayofyear = dn-dnstart+1Result = sprintf('%4s%03d', dt_str(1:4),dayofyear)Result =2011150The +1 is necessary for ‘dayofyear’ because it computes the difference between 01-Jan-2011 and doesn’t include 01-Jan-2011.EDIT — Added ‘Result’ assignment. Related QuestionHow to convert numbers in the form yyyymm to dates in the form mm/dd/yyyyHow to convert yymmdd to dd/mm/yyyyImport date from matlab to ms excel in the form ‘HH:MM PM’ using xlswrite
Best Answer