Add Interactive Controls to a Live Script
You can add sliders, drop-downs, check boxes, edit fields, and buttons to your live scripts to control variable values interactively. Adding interactive controls to a script is useful when you want to share the script with others. Use interactive controls to set and change the values of variables in your live script using familiar user interface components.
To insert a control into a live script, go to the Live Editor tab, and in the Code section, click Control. Then, select from the available options. To replace an existing value with a control, select the value and then insert the control. The Control menu only shows options available for the selected value.
The table below shows the list of available controls:
Use numeric sliders to interactively change the value of a variable by moving the slider thumb to the desired numeric value.
The value to the left of the slider is its current value.
In the Values section, specify a Min, Max, and Step value or select a workspace variable from the drop-down list.
For more information about specifying the slider values using variables, see Link Variables to Controls.
Use drop-down lists to interactively change the value of a variable by selecting from a list of values.
Hover over the text displayed in the drop-down list to see its current value.
In the Items > Item Labels field, specify the text that you want to display for each item in the drop-down list.
In the Items > Item Values field, specify the values for each item in the drop-down line. Make sure to enclose text values in quotes or double quotes, because the Live Editor interprets each item in the list as code.
You also can populate the items in the drop-down list using values stored in a variable. For more information, see Link Variables to Controls.
Use check boxes to interactively set the value of a
variable to either the logical value
The displayed state of the check box (checked or not checked) determines its current value.
Use edit fields to interactively set the value of a variable to any typed input.
The text displayed in the edit field and the selected data type determines its current value.
In the Type section, in the Data type field, select from the available options to specify the data type of the text in the edit field.
Use button controls to interactively run code on button click.
When using button controls, consider setting the Run field for all other controls in the live script to None. Then, the code only runs when the user clicks the button control. This can be useful when the live script requires multiple control values to be set before running the code.
To change the label displayed on the button, in the Label section, enter a label name.
Modify Control Labels
You can hide the code in a live script and only display formatted text, labeled controls, Live Editor tasks, and output. Hiding the code is useful when sharing and exporting live scripts. To hide the code, click the Hide code button to the right of the live script. You also can go to the View tab, and in the View section, click Hide Code . To show the code again, click the Output inline button or the Output on right button .
When the code is hidden, labels display next to the control. To modify the label for a control, right-click the control and select Configure Control. Then, in the Label section, enter a label name. This is also the text that displays on button controls in all views. Press Tab or Enter, or click outside of the control configuration menu to return to the live script.
Link Variables to Controls
You can link variables to slider values and drop-down items to create dynamic controls.
To specify the minimum, maximum, and step values for a slider using variables, right-click the control and select Configure Control. Then, in the Values section, select a workspace variable for Min, Max, and Step. Only variables with numeric values appear in the drop-down list. If the variables that you want to select are not listed, try running the live script first to create the variables and add them to the workspace. Changes to the variables are automatically reflected in the numeric slider.
To populate the items in a drop-down list using the values stored in a variable, right-click the control and select Configure Control. Then, in the Items section, select a workspace variable from the Variable list. The variable must be a string array or a string scalar to appear in the list. If the variable that you want to select is not listed, try running the live script first to create the variable and add it to the workspace. Changes to the variable are automatically reflected in the drop-down list.
For example, create a live script and define the variable
lastnames containing a list of last
lastnames = ["Houston","Vega","Obrien","Potter","Rivera","Hanson","Fowler","Tran","Briggs"];
Run the live script to create
lastnames and add it to the
workspace. Then, go to the Live Editor tab, and in the
Code section, select Control > Drop Down. In the Items section of the control
configuration menu, select
lastnames as the
Close the configuration menu to return to the live script. The drop-down list now
contains the last names defined in
If you add, remove, or edit the values in
MATLAB® updates the items in the drop-down list accordingly.
If the items in a drop-down list are linked to a variable, and one or more of the values in the variable are deleted while MATLAB is running, an error can occur if one of the deleted values was the selected list item. To prevent the error from occurring, avoid deleting values from a linked variable while the live script is running.
Specify Default Values
You can set the default value for sliders, drop-down lists, check boxes, and edit fields in live scripts.
To set the default value for a control, right-click the control and select Configure Control. Then, in the Defaults section, specify a default value by entering the value or by selecting a workspace variable from the list. The list shows only valid variables for the control. For drop-down lists, select the default value from the list of items.
To restore the default value for a control, right-click the control and select Restore Default Value.
To link the value of a control to a workspace variable, set the default value for the control to that variable. The control value is set to the default value and changes as the default value changes. The control value stays linked to the default value until the control value is changed manually, for example, by moving the slider thumb of a numeric slider.
Modify Control Execution
You can modify when and what code runs when the value of a control changes. By default, when the value of a control changes, the Live Editor runs the code in the current section. To configure this behavior, right-click the control and select Configure Control. Then, in the Execution section, modify the values of the fields described in the table below. Press Tab or Enter, or click outside of the control configuration menu to return to the live script.
|Run On (slider control only)|
Select one of these options to specify when the code runs:
Select one of these options to specify what code runs when the value of the control changes:
When using a button control in a live script, consider setting the Run field for all other controls in the live script to Nothing. Then, the code only runs when the user clicks the button control. This can be useful when the live script requires multiple control values to be set before running the code.
Create Live Script with Multiple Interactive Controls
This example shows how you can use interactive controls to visualize and investigate patient data in MATLAB®. The example plots the age and systolic blood pressure of patients at a specified location and highlights the patients over a specified blood pressure.
To specify the location of the patients to plot, insert a drop-down list and select the
locationStrings variable to populate the items in the list. To specify a threshold systolic blood pressure, insert a numeric slider and select the
maxPressure variables as the Min and Max values.
To view and interact with the controls, open this example in your browser or in MATLAB.
load patients locationStrings = ["County General Hospital", "VA Hospital","St. Mary's Medical Center"]; selectedLocation = locationStrings(1); minPressure = min(Systolic); maxPressure = max(Systolic); thresholdPressure = 132; overThresholdAges = Age(Location==selectedLocation & Systolic>=thresholdPressure); overThresholdPressures = Systolic(Location==selectedLocation & Systolic>=thresholdPressure); sp1 = scatter(Age(Location==selectedLocation),Systolic(Location==selectedLocation),'blue'); hold on sp2 = scatter(overThresholdAges, overThresholdPressures,'red'); hold off title("Age vs. Systolic Blood Pressure of Patients at " + selectedLocation); xlabel("Age") ylabel("Systolic Blood Pressure") legendText = sprintf("Patients with systolic pressure over %d mmHg.",thresholdPressure); legend(sp2,legendText,Location="southoutside")
Share Live Script
When the live script is complete, share it with others. Users can open the live script in MATLAB and experiment with using the controls interactively.
If you share the live script itself as an interactive document, consider hiding the code in the live script before sharing it. When the code is hidden, the Live Editor only displays formatted text, labeled controls, Live Editor tasks, and output. To hide the code, click the Hide code button to the right of the live script. You also can go to the View tab, and in the View section, click Hide Code .
If you share the live script as a static PDF, Microsoft® Word, HTML, or LaTeX document, the Live Editor saves the control as code.
For example, in this live script exported to HTML, the drop-down list has been
replaced with its current value (
locationStrings(1)) and the
slider control has been replaced with its current value