How To Get Data From ValueChangedFcn in an Uidatepicker?

16 views (last 30 days)
Hi, Community,
I have a problem to get data value from ValueChangedFcn properties... So i have functions below :
dating_timing_original = datetime vector...
cals = datetime(dating_timing_original, 'InputFormat', 'yyyy-MM-dd HH:mm:ss', 'Format', 'yyyy-MM-dd');
mydateapp(cals);
function ff = mydateapp(cals)
fig = uifigure('Position',[200 300 400 300]);
d = uidatepicker(fig,'DisplayFormat','yyyy-MM-dd',...
'Position',[130 190 152 30],...
'Value',min(cals),...
'Limits', [min(cals) max(cals)],...
'ValueChangedFcn', @datechange);
outs = d.ValueChangedFcn;
ff = outs;
function [outs] = datechange(src, event)
% fig2 = uifigure('Position',[130 190 150 22]);
lastdate = char(event.PreviousValue);
newdate = char(event.Value);
if newdate == lastdate
newdate = lastdate
end
% msg = ['Change date from ' lastdate ' to ' newdate '?'];
% Confirm new date
% selection = uiconfirm(fig2,msg,'Confirm Date');
outs = string(datetime(newdate, 'Format', 'yyyy-MM-dd HH:mm:ss'));
src.ValueChangedFcn = outs;
How to get 'ValueChangedFcn' data as an output of mydateapp(cals) function so i can get the value of datetime data as result of clicked calendar (uidatepicker).... Thank you everyone...
  2 Comments
Geoff Hayes
Geoff Hayes on 1 Apr 2022
@Tyann Hardyn - can you add some more details on how the above code is to be used? Do you have a GUI (if so, was it designed with App Designer, GUIDE, programmatically?) or are you using a script or function to do some work?
Tyann Hardyn
Tyann Hardyn on 2 Apr 2022
Alright, Sir.. Iam using GUIDE to create a uidatepicker right after my pushbutton was clicked

Sign in to comment.

Accepted Answer

Benjamin Kraus
Benjamin Kraus on 1 Apr 2022
When you create a uidatepicker the object is created immediately, and the next line of code continues to run. It does not wait for you to interact with the date picker. In order to stop code execution until the user selects a date, you need to use something like waitfor. In addition, the ValueChangedFcn is code that will run when the user changes the value selected in the date picker, but it can't be queried to get the current value, and you shouldn't be setting the ValueChangedFcn to be a date.
It looks like you are trying to create a model dialog that prompts for a date, and pause code execution while waiting for an answer.
Option 1
This code will do something like that:
cals = datetime()+days(1:10);
d = pickadate(cals)
function selectedDate = pickadate(cals)
% Create a modal figure
fig = uifigure('WindowStyle','modal');
% Set the initial date stored in App data. You can use all sorts of options
% for storing this answer, including a property on another figure or App.
% I'm using app data because it doesn't require any other objects.
setappdata(fig, "SelectedDate", min(cals));
% Create a date picker. Make sure the ValueChangedFcn updates whatever
% location you want to store the output value in.
d = uidatepicker(fig,'DisplayFormat','yyyy-MM-dd',...
'Position',[130 190 152 30],...
'Value',min(cals),...
'Limits', [min(cals) max(cals)],...
'ValueChangedFcn', @(obj,~) setappdata(fig, "SelectedDate", obj.Value));
% When you attempt to close the figure, instead of actually closing the
% figure, set the WindowStyle to be 'normal'. This will be used as a
% trigger to tell 'waitfor' to continue.
fig.CloseRequestFcn = @(f,~) set(f,'WindowStyle','normal');
% Add an OK button. Not required, but helpful for users.
uibutton(fig, "Text", "OK", "ButtonPushedFcn", @(~,~) close(fig));
% Wait until the WindowStyle changes to 'normal', which signals that the
% user has finished selecting a date.
waitfor(fig,'WindowStyle','normal');
% Read the date the user selected.
selectedDate = getappdata(fig, "SelectedDate");
% Because the CloseRequestFcn did not close the figure, you need to
% manually delete it here, otherwise it will never close.
delete(fig);
end
Note: It is easy to get stuck when testing this code due to the custom CloseRequestFcn. If you get stuck, you can close the figure with code like this:
f = findall(groot,'type','figure');
delete(f);
Option 2
Another approach you may want to use requires that there is another figure (or app) available that can be used to store the value the user selected. This approach requires a place to store the selected date outside the figure with the date picker, but it might be safer because it doesn't overload the CloseRequestFcn. For this approach, I used a dynamic property on another figure, but if you were using an App or another object it doesn't need to be a dynamic property.
cals = datetime()+days(1:10);
mainFig = uifigure;
d = pickadate(mainFig, cals)
function selectedDate = pickadate(mainFig, cals)
% Create a dynamic property on another figure to store the date selected.
addprop(mainFig, "SelectedDate");
mainFig.SelectedDate = min(cals);
% Create a modal figure for the date picker.
fig = uifigure('WindowStyle','modal');
% Create a date picker. Make sure the ValueChangedFcn updates whatever
% location you want to store the output value in.
d = uidatepicker(fig,'DisplayFormat','yyyy-MM-dd',...
'Position',[130 190 152 30],...
'Value',min(cals),...
'Limits', [min(cals) max(cals)],...
'ValueChangedFcn', @(obj,~) set(mainFig, "SelectedDate", obj.Value));
% Add an OK button. Not required, but helpful for users.
uibutton(fig, "Text", "OK", "ButtonPushedFcn", @(~,~) close(fig));
% Wait until the model figure window closes.
waitfor(fig);
% Read the date the user selected.
selectedDate = mainFig.SelectedDate;
end
  1 Comment
Tyann Hardyn
Tyann Hardyn on 2 Apr 2022
OMG, there is even 2 Options to answer my question... Thank you so Much, Sir... Iam so grateful, there are so many genius people who re kindly helped a newbie programmer like me... I ll try it Sir...

Sign in to comment.

More Answers (0)

Categories

Find more on Polar Plots 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!