Create Live Functions
Live functions are program files that contain code and formatted text together in a single interactive
environment called the Live Editor. Similar to live scripts, live functions allow you to reuse sequences
of commands by storing them in program files. Live functions provide more flexibility, though,
primarily because you can pass them input values and receive output values.
Create Live Function
To create a live function, go to the Home tab and select New > Live Function.
Open Existing Function as Live Function
If you have an existing function, you can open it as a live function in the Live Editor. Opening a
function as a live function creates a copy of the file and leaves the original file untouched. MATLAB
converts publishing markup from the original script to formatted content in the new live function.
To open an existing function (.m) as a live function (.mlx) from the Editor, right-click the document
tab and select Open functionName as Live Function from the context menu.
Alternatively, go to the Editor tab, click Save , and select Save As. Then, set the Save as type: to
MATLAB Live Code Files (*.mlx) and click Save.
Note You must use one of the described conversion methods to convert your function to a live
function. Simply renaming the function with a .mlx extension does not work and can corrupt the file.
Create Live Function from Selected Code
If you have an existing large live script or function, you can break it into smaller pieces by
automatically converting selected areas of code into functions or local functions. This is called code
refactoring.
To refactor a selected area of code, select one or more lines of code and on the Live Editor tab, in
the Code section, click
Refactor. Then, select from the available options. MATLAB creates a
function with the selected code and replaces the original code with a call to the newly created
function.
Add Code
After you create the live function, add code to the function and save it. For example, add this code
and save it as a function called mymean.mlx. The mymean function calculates the average of the
input list and returns the results.
function a = mymean(v,n)
a = sum(v)/n;
end
19 Live Scripts and Functions
19-50
Précédent

- 668/1450

Suivant