To define a callback function, simply make a function in the same file as the GUI frame. The syntax for this is a little different from typical function usage. It is the responsibility of the programmer to provide that function, and to tell MATLAB how to call that function. When actions are to be performed as a result of clicking on a control, MATLAB places a call to a “callback function”. % Cover with buttons c = 1 for xpos = 1:9 for ypos = 1:9 % These are drawn on the figure, not any panel u(c)=uicontrol('Style', 'pushbutton', 'Units', 'normalized') set(u(c), 'Position', ) c = c + 1 end We now have a visually-complete game. We will change that after we have a visually complete GUI – then we can make it fully functional. % Control to hold text (to display after button is pushed) tbh = uicontrol('Units', 'normalized', 'Style', 'text') set(tbh, 'String', '8', 'FontSize', 16) set(tbh, 'Position', ) Now that we have one box, let’s make the code repeat it.Ĭ = 1 for xpos = 1:9 for ypos = 1:9 % Control to hold text (to display after button is pushed) tb(c) = uicontrol('Style', 'text', 'Units', 'normalized') set(tb(c), 'String', '8', 'FontSize', 16) set(tb(c), 'Position', ) c = c+1 end % of for ypos end % of for xpos (We assume a 9x9 grid – this can become a feature selected by the user) These all show the number ‘8’. The button % will disappear when clicked. If you have multiple frames in your interface, functions permit separation of code by allowing a main program to instantiate the frames independently.ģ Create a basic figure function MineSweeper_GUI() fh = figure('Position', ) % Adjust appearance set(fh, 'Resize', 'off') set(fh, 'MenuBar', 'none') set(fh, 'NumberTitle', 'off') set(fh, 'Name', 'MATLAB MineSweeper') end % of MineSweeper_GUI() These are common attributes, although not every GUI will necessarily set them.Ĥ Provide the controls % Text will be provided and covered up by a button. MATLAB Minesweeper Development of a MATLAB GUIįunction MineSweeper_GUI() end % of MineSweeper() While not strictly required, a function that creates the GUI provides flexibility for your program.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |