matlab.task.LiveTask.reset - Reset instance of Live Editor task subclass - MATLAB (original) (raw)

Class: matlab.task.LiveTask
Namespace: matlab.task

Reset instance of Live Editor task subclass

Since R2022a

Description

reset([obj](#mw%5F8c695733-83b0-4686-a1f3-3bd818eb7076)) resets the state of the task. It executes when a user restores the default values of the task by clicking the options button at the top-right of the task and selecting . Define this method in a method block with no arguments, along with the get.Summary, get.State,set.State, and generateCode methods.

Input Arguments

expand all

Object of the class that inherits from the matlab.task.LiveTask base class.

Examples

expand all

Define a class called DisplayImage that creates a custom Live Editor task for displaying an image.

To define the class, create a file called DisplayImage.m that contains the following class definition with these features:

classdef DisplayImage < matlab.task.LiveTask properties(Access = private,Transient) FileNameEditField matlab.ui.control.EditField BrowseButton matlab.ui.control.Button end

properties(Dependent)
    State
    Summary
end

methods(Access = protected)
    function setup(task)
        createComponents(task);
        setComponentsToDefault(task);
    end
end

methods
    function [code,outputs] = generateCode(task)
        if isempty(task.FileNameEditField.Value)
            % Return empty values if there is not enough
            % information to generate code
            code = "";
            outputs = {};
            return
        end
        
        outputs = {"im"};
        code = ["% Get Image"
            outputs{1} + " = imread(""" + ...
            task.FileNameEditField.Value + """);"
            ""
            "% Visualize results"
            "figure"          
            "imshow(" + outputs{1} + ");"];
    end
    
    function summary = get.Summary(task)
        if isempty(task.FileNameEditField.Value)
            summary = "Display selected image";
        else
            [~,name,~] = fileparts(task.FileNameEditField.Value);
            summary = "Display image '" + name + "'";
        end
    end

    function state = get.State(task)
        state = struct;
        state.FileNameEditFieldValue = task.FileNameEditField.Value;
    end

    function set.State(task,state)
        task.FileNameEditField.Value = state.FileNameEditFieldValue; 
    end
    
    function reset(task)
        setComponentsToDefault(task);
    end
end

methods(Access = private)
    function createComponents(task)
        task.LayoutManager.RowHeight = ["fit" "fit" "fit"]; 
        task.LayoutManager.ColumnWidth = "fit";

        % Row 1: Select image section label
        uilabel(task.LayoutManager,"Text","Select image", ...
            "FontWeight","bold");

        % Row 2: Select data section components
        inputgrid = uigridlayout(task.LayoutManager,"RowHeight", ...
            "fit","ColumnWidth",{"fit",200,"fit"},"Padding",0);
        uilabel(inputgrid,"Text","Input image");
        task.FileNameEditField = uieditfield(inputgrid, ...
            "Editable",false);            
        task.BrowseButton = uibutton(inputgrid,"Text","Browse", ...
           "ButtonPushedFcn",@task.inputImageFile);
        
        % Row 3: Display results section label
        uilabel(task.LayoutManager,"Text","Display results", ...
            "FontWeight","bold");
    end

    function setComponentsToDefault(task)
        task.FileNameEditField.Value = "";    
    end

    function inputImageFile(task,~,~)
        % Display uigetfile dialog box
        filterspec = ["*.jpg;*.tif;*.png;*.gif","All Image Files"];
        [f,p] = uigetfile(filterspec);
        
        % Make sure user did not cancel uigetfile dialog box
        if (ischar(p))
           fileName = [p f];
           task.FileNameEditField.Value = fileName;
        end
        
        notify(task,"StateChanged");
    end
end

end

Next, configure the task metadata by calling the matlab.task.configureMetadata function and selecting the DisplayImage.m file. The Task Metadata dialog box opens with all of the required task metadata details prepopulated.

Select OK to use the prepopulated metadata details. MATLAB creates a folder named resources inside the folder containing your task class definition file. Inside the resources folder, MATLAB generates a file named liveTasks.json.

Add the folder containing the task class definition file to the MATLAB path by calling the addpath function or using the Add Folder button in the Set Path dialog box.

Add the task to a live script. On a code line, type display. MATLAB shows a list of suggested matches.

Select Display Image from the list. MATLAB adds the Display Image task to the live script.

Version History

Introduced in R2022a