Main Content

Add Push Button to View Pin Map

This example shows how to use matlab.system.display.Action to add a button to the block mask. When you click the button, an image of the hardware board pin map opens. The example shows how to set button options and use an actionData object input to store a figure handle. That way, the same figure is used when the button is clicked multiple times, rather than opening a new figure for each button click.

  1. Add the function viewpinmap to the methods of System object™.

    methods
        function viewpinmap(obj,actionData)
            f = actionData.UserData;
            if isempty(f) || ~ishandle(f)
                
                % Create figure for pin map.
                f = figure;
                f.Name = 'Hardware Pin Map';
                f.NumberTitle = 'off';       % Remove number title
                f.ToolBar = 'none';          % Remove toolbar
                f.MenuBar = 'none';          % Remove menubar
    
                % Create axes in figure.
                ax = axes(f);                 
                ax.Position = [0 0 1 1];      % Set axes to fill figure
    
                % Display the image.
                imshow( 'peppers.png',...
                    'Parent',ax,...
                    'InitialMagnification','fit');
              
                actionData.UserData = f;
            else
                figure(f); % Make figure current.
            end
        end
    end

    This function creates a figure that contains the image from the peppers.png file. This image should be replaced with the pin map image for the hardware board.

  2. Add the following getPropertyGroupImpl function to the methods of the System object.

    methods(Static,Access=protected)
        function group = getPropertyGroupsImpl
            group = matlab.system.display.Section(mfilename('class'));
            group.Actions = matlab.system.display.Action(@(actionData,obj) ...
                viewpinmap(obj,actionData),'Label','Pin Map');
        end
        ...
    end

    This function creates the button on the block mask and executes the function viewpinmap when clicked.

  3. Click the button to confirm the figure with the pin map image displays to the screen.

Note

Because the Digital Write block depends directly on the hardware, it is recommended that you add a block dialog box button that shows users the pin map of the hardware board.

See Also

| | |