%Jonathan Dorfman, Jan. 18, 2005 (Math128B) %provides interface for creating a schedule % represented by a 16x5 matrix of 0's and 1's % and saved as file "schedule128.mat" %adapted from Ch.6 "Graphics and GUIs with Matlab" by Patrick Marchand function Scheduler(cmdStr, Minit) if (nargin == 0) cmdStr = 'init' Minit = ones(16,5); %initially all times are available end switch cmdStr case 'init' M = Minit; Fig = figure; Axes = axes; title('Spring 2005 Schedule'); %create the "Save" button uicontrol('style','pushbutton','string','Save',... 'units','normalized','pos',[.8 .93 .1 .05],... 'callback','Scheduler(''saveptr'')'); %set Figure properties set(Fig,'ColorMap',[0 0 0; .5 .5 .5; 1 1 1]); %black, or grey or white set(Fig,'DoubleBuffer','on'); %remove flashing set(Fig,'UserData',M); %globally accessible storage %create the 16x5 array of patch handles for (i=1:5) for (j=1:16) color = M(j,i) + 1; h = patch([i i+1 i+1 i],... 17.5 - [j j j+1 j+1], M(j,i)*[1 1 1 1]); set(h,'cdata',color*ones(1,4)); %the patch's color - initially all grey set(h,'userdata',[j i]); %each patch can return its (i,j)-position set(h, 'buttondownfcn', 'Scheduler(''editblock'')'); end end %set Axes properties axis([1 6 .5 17.5]); %5 days of week <--> 1.5, 2.5, 3.5, 4.5, 5.5 set(Axes,'clim', [1 3]); %only 3 colors in the colormap (black, white, grey) set(Axes,'XTickLabel',{'','Mon','','Tue','','Wed','','Thu','','Fri',''}) set(Axes,'YTickLabel',{'4pm','3pm','2pm','1pm','12pm','11am','10am','9am'}) case 'editblock' %get info about patch responding to the ButtonDownEvent h = get(0,'callbackobject'); c = get(h,'cdata'); %patch identifies its old color ij = get(h,'userdata'); %patch identifies its (i,j)-position %update color color = 3 - c; %toggle color (black <--> gray, 1 <--> 2) set(h,'cdata',color); %update patch's color with toggled value %update schedule array M = get(gcf,'UserData'); M(ij(1,1),ij(1,2)) = color(1) - 1; %M(i,j) (black -> 0, gray -> 1) set(gcf,'UserData',M); case 'saveptr' M = get(gcf,'UserData'); [file, pathname] = uiputfile('schedule128.mat','Save Schedule'); save([pathname, file], 'M'); otherwise errordlg(['error, no such command: ', cmdStr]); end return