Thank you all for the advice. The function is now performing as desired with uiwait. I've included the final version which also includes format verfication using try/catch that ensures the GUI does not close until the dates are provided correctly.
function [startDate, stopDate] = dateInputGUI()
% Set default start and stop dates
defaultStartDate = datestr(datetime('today'), 'dd mmm yyyy HH:MM:SS.FFF');
defaultStopDate = datestr(datetime('tomorrow'), 'dd mmm yyyy HH:MM:SS.FFF');
% Create figure and components
f = figure('Position', [400, 400, 400, 220], 'Name', 'Date Input');
uicontrol('Style','text','String','Expected format: dd mmm yyyy HH:MM:SS.FFF','Position',[50,180,260,20]);
uicontrol('Style', 'text', 'String', 'Start Date:', 'Position', [50, 140, 70, 20]);
startDateEdit = uicontrol('Style', 'edit', 'Position', [130, 140, 220, 20],'String',defaultStartDate);
uicontrol('Style', 'text', 'String', 'Stop Date:', 'Position', [50, 100, 70, 20]);
stopDateEdit = uicontrol('Style', 'edit', 'Position', [130, 100, 220, 20],'String',defaultStopDate);
uicontrol('Style', 'pushbutton', 'String', 'Submit', 'Position', [110, 50, 80, 30], 'Callback', @submitCallback);
% wait until the figure is deleted
uiwait(f);
% Callback function for submit button
function submitCallback(~,~)
startDate = get(startDateEdit, 'String');
stopDate = get(stopDateEdit, 'String');
%Convert to datetime to confirm string is in the correct format and
%that the dates are in order.
try
startDateObj = datetime(startDate, 'InputFormat', 'dd MMM yyyy HH:mm:ss.SSS');
stopDateObj = datetime(stopDate, 'InputFormat', 'dd MMM yyyy HH:mm:ss.SSS');
% Check if start date is before stop date
if startDateObj < stopDateObj
disp('Dates are valid and start date is before stop date.');
delete(f);
else
disp('Error: Start date should be before stop date.');
end
catch
disp('Error: Invalid date format. Please use "dd mmm yyyy HH:MM:SS.FFF".')
end
end
end