uicontrol assigning values a a user defined date
Show older comments
Hey im trying to make a function that lets the user define a date from a list of pop up choices. The menus come out fine but i dont know how to assign them to anything. any help is much appreciated, thanks!
here is what i have so far:
function [userdate]= datepicker (~)
figure;
uicontrol('units','normalized','position',[0.1,0.2,0.3,0.05],...
'style','popup','string',{'January','February','March','April','May',...
'June','July','August','September','October','November','December'},...
'value',1,'callback',@changetype);
hm2 = uicontrol('units','normalized','position',[0.6,0.2,0.3,0.05],...
'style','popup','string',{''},'value',1);
function changetype(hObj,~)
if get(hObj,'value')==2
s = {1:29};
else
switch mod(get(hObj,'value'),2)
case 0
s = {1:30};
case 1
s = {1:31};
end
end
set(hm2,'string',s,'callback',@changetype)
end
end
1 Comment
Accepted Answer
More Answers (1)
Akiva Gordon
on 9 Nov 2012
A couple of things:
- The switch/case algorithm you have there does not hold true for many of the months, e.g. there are 31 days in August. A few others are not quite right either.
- The callback for "hm2" should not be "changetype", because this means that if the user selects "2" for the date, then that list changes to 29 days regardless of the month. Unless you want something particular to happen when the user picks a certain date, you don't really need to set a callback there.
- In order to use the values, you might want to consider capturing the handle of the "Month" uicontrol. In addition, you can make the data easier to manage by creating a "handles" structure, such as "handles.month = uicontrol(...)" and "handles.day = uicontrol(...)". You can then use these handles later to get the current user choice like this:
userMonth = get(handles.month,'String')
userDay = get(handles.day ,'Value' )
You now have two variables, "userMonth" and "userDay" that you can use.
Categories
Find more on Grid Lines, Tick Values, and Labels 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!