Matlab uifigure usage

fig = uifigure creates a figure for building a user interface and returns the Figure object. This is the type of figure that App Designer uses.

fig = uifigure(Name,Value) specifies figure properties using one or more Name,Value pair arguments.

Create Default UI Figure

fig = uifigure;

Set and Access Properties

Create a UI figure with a specific title

fig = uifigure('Name','Plotted Results');
Get the Position property value.
p = fig.Position
ans =

   680   678   560   420

Code CloseRequestFcn to Confirm Closing UI Figure

Code the CloseRequestFcn callback to open a modal Confirmation dialog box when the user tries to close the window.

Copy and paste this code into the MATLAB® Editor, and then run closeFig.

function closeFig

fig = uifigure('Position',[100 100 425 275]);
fig.CloseRequestFcn = @(fig,event)my_closereq(fig);

    function my_closereq(fig,selection)
        selection = uiconfirm(fig,'Close the figure window?',...
        switch selection
            case 'OK'
            case 'Cancel'


Click the figure close button. The Confirmation dialog box opens.

Change Mouse Pointer Symbol

Change the mouse pointer symbol that displays when you hover over a push button.

This program file, called setMousePointer.m, shows you how to:

  • Create a UI figure which executes custom code when the mouse is moved over a button. To do this, use the @ operator to assign the mouseMoved function handle to the WindowButtonMotionFcn property of the figure.
  • Create a push button and specify its coordinates and label.
  • Create a callback function called mouseMoved with the custom code you want to execute when the mouse moves over the button. In the function, query the CurrentPoint property to determine the mouse pointer coordinates. Set the Pointer property to 'hand' if the pointer coordinates are within the push button coordinates.

Run setMousePointer. Then move the mouse over the push button to see the mouse pointer symbol change.

function setMousePointer
    fig = uifigure('Position',[500 500 375 275]);
    fig.WindowButtonMotionFcn = @mouseMoved;

    btn = uibutton(fig);
    btnX = 50;
    btnY = 50;
    btnWidth = 100;
    btnHeight = 22;
    btn.Position = [btnX btnY btnWidth btnHeight];
    btn.Text = 'Submit Changes';

      function mouseMoved(src,event)
          mousePos = fig.CurrentPoint;

          if  (mousePos(1) >= btnX) && (mousePos(1) <= btnX + btnWidth) ...
                        && (mousePos(2) >= btnY) && (mousePos(2) <= btnY + btnHeight)

              fig.Pointer = 'hand';

              fig.Pointer = 'arrow';



For more: you might also be interested in :

MATLAB GUI (Graphical User Interface) Tutorial for Beginners

Back to top button