MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

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

Add Help


To document the function, add formatted help text above the function definition. For
example, add a title and some text to describe the functionality. For more information
about adding help text to functions, see “Add Help for Live Functions” on page 19-65.

Run Live Function


You can run live functions using several methods, including calling them from the
Command Window or calling them from a live script.

To run a live function from the Command Window, enter the name of the function in the
Command Window. For example, use mymean.mlx to calculate the mean of 10 sequential
numbers from 1 to 10.

mymean(1:10, 10)

ans =
5.5000

19 Live Scripts and Functions

Free download pdf