uicontrol assigning values a a user defined date

5 views (last 30 days)
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
H Rincon
H Rincon on 1 Dec 2012
Edited: H Rincon on 1 Dec 2012
Thank you both! I actually learned about eomday the week after, i guess i was just getting ahead of myself =p

Sign in to comment.

Accepted Answer

Matt Fig
Matt Fig on 9 Nov 2012
Edited: Matt Fig on 9 Nov 2012
Here is one way to make the code return a value.
function [userdate]= datepicker (~)
figure('units','pix',...
'pos',[500 500 400 200 ],...
'numbertitle','off',...
'name','Datepicker',...
'menubar','none');
S = {'Month','January','February','March','April',...
'May','June','July','August','September',...
'October','November','December'};
hm1 = uicontrol('units','normalized',...
'position',[0.1,0.2,0.3,0.05],...
'style','popup',...
'string',S,...
'value',1,...
'callback',@changetype);
hm2 = uicontrol('units','normalized',...
'position',[0.6,0.2,0.3,0.05],...
'style','popup',...
'string',{''},...
'value',1,...
'callback',@changetype,...
'enable','off');
uiwait(gcf)
function changetype(hObj,~)
s = ['Day|',sprintf('%i|',1:eomday(2012,get(hm1,'val')-1))];
set(hm2,'string',s,'enable','on')
set(hm1,'enable','off')
if hObj==hm2
s = get(hm2,'string');
Str = [S{get(hm1,('val'))},' ',s(get(hm2,('val')),:)];
userdate = Str;
uiresume(gcbf)
close(gcbf)
end
end
end

More Answers (1)

Akiva Gordon
Akiva Gordon on 9 Nov 2012
A couple of things:
  1. 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.
  2. 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.
  3. 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 Migrate GUIDE Apps 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!