uicontrol assigning values a a user defined date

조회 수: 1 (최근 30일)
H Rincon
H Rincon 2012년 11월 9일
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