uicontrol assigning values a a user defined date

1 次查看(过去 30 天)
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 个评论
H Rincon
H Rincon 2012-12-1
编辑:H Rincon 2012-12-1
Thank you both! I actually learned about eomday the week after, i guess i was just getting ahead of myself =p

请先登录,再进行评论。

采纳的回答

Matt Fig
Matt Fig 2012-11-9
编辑:Matt Fig 2012-11-9
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

更多回答(1 个)

Akiva Gordon
Akiva Gordon 2012-11-9
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.

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by