Check date with a given format in Matlab

I am designing a graphical interface. I have an edit text box into which I entered a date string with the following format: "March 31, 2011 10: 00: 00.000". I need a code to check it and write an error message in case of invalid input. Thank you for your attention. Greetings.

+4
source share
4 answers

Could you handle this regex. If you require the user to enter a date in a specific format, I could see the parsing of this string using the matlab regexp function to see if it matches the given format. In the case when you gave something like

s='31 Mar 2011 10:00:00.000' regexp(s,'\d{1,2} \w{3} \d{4} \d{2}:\d{2}:\d{2}\.?\d*') 

can be used to see if your date matches the format.

+4
source

Wrap a datenum in try - catch , for example:

 try a=datenum('lkdsldkjhfsg'); catch disp('Bad date') end 
+4
source

You can take a look at datestr , datenum , predefined date formats and symbolic identifiers for the field . You can then create a validation around each of the identifiers by checking to see if it meets certain criteria. You can use error to cause an error for the user in case of invalid input.

+2
source

Thanks everyone for the answers. This is one of the possible function callback codes corresponding to the edit text field that you can use:

 user_entry = get(hObject,'string'); control = regexp(user_entry,'\d{1,2} \w{3} \d{4} \d{2}:\d{2}:\d{2}\.?\d*') if(numel(control)==0) errordlg('Invalid Input Format','Error Message','modal') uicontrol(hObject) end 
+1
source

Source: https://habr.com/ru/post/1347814/


All Articles