generateCode
Class: matlab.task.LiveTask
Namespace: matlab.task
Syntax
[code,taskoutputs] = generateCode(obj)
Description
[
generates the MATLAB® commands and output for the task. This method executes when the state of the
task changes. The generated code displays in the code section of the task. When the live
script section containing the task runs, the Live Editor uses the generated code to run the
task. Define this method in a method block with no arguments, along with the
code
,taskoutputs
] = generateCode(obj
)get.Summary
, get.State
, set.State
, and
reset
methods.
Input Arguments
obj
— Object of the class
task object
Object of the class that inherits from the matlab.task.LiveTask
base
class.
Output Arguments
code
— Generated code for the task
string array | character array
Generated code for the task, returned as a string array or character array.
taskoutputs
— Output variables generated by the task
cell array
Output variables generated by the task, returned as a cell array. If the task does
not generate output, return taskoutputs
as an empty cell
array.
Examples
Display Image Using Custom Live Editor Task
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:
State
andSummary
public properties that store the current state of the task and a dynamic summary of what the task does.Private properties that store the edit field and button for selecting an image.
A
setup
method that initializes the task.A
generateCode
method that updates the generated code for the task when the selected image changes.get.Summary
,get.State
, andset.State
methods for getting and setting the summary and state of the task.An
inputImageFile
method that prompts the user to select an image to display.A
reset
method that resets the state of the task.
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
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)