Task: Create a general plotting function which accepts input arrays x and y and creates a line plot. Allow an optional argument that controls line color and line style. Write a script to demonstrate that the function works as expected.
Functions can have optional arguments for input or output. This allows functions to have default behaviors with simple input, but more complicated behaviors if desired.
When a function is invoked, MATLAB defines two variables (nargin and nargout) inside the function which give the number of input and output variables, respectively.
Consider the following simple script that adds two numbers, or adds 1 to a single number.
function s = MyAdd(a,b) % add the two arguments, or add 1 to a single argument % calling sequence: s = MyAdd(a,b). if (nargin > 2) error('MyAdd: too many input arguments') elseif (nargin < 1) error('MyAdd: not enough input arguments') elseif (nargin == 1) s = a + 1; elseif (nargin == 2) % don't need this test as it is the only remaining option s = a + b; endSimilar procedures can be used for different numbers of output arguments.
Although it is considered poor programming practice, it is possible to break the isolation of variables in functions with the keyword global which defines variables that are known both inside and outside of the function. You might use this to define variables that are needed in several functions. Defining key values in several places is a dangerous practice which can lead to errors that are very hard to find.
Suppose you need the value of gravitational acceleration in several functions. It is made generally available with global. Notice that the name must be the same in all places.
% Script to calculate weight from mass. global gravity % declare a global variable gravity = 9.8; % m/s^2 mass = 21; % kg weight=CalcWeight(mass);The function is defined in CalcWeight.m:
function W = CalcWeight(M) % calculate the weight (in Newtons) of an object given % its mass (in kg). global gravity % declare a link to a global variable W = gravity*M;
The flow chart for the task solution is
%%% Define test conditions %%% define the function %%% add documentation for the function %%% test for four input arguments %%% create line plot with chosen color and style %%% test for three input arguments %%% create line plot with chosen color %%% test for two input arguments %%% create line plot %%% otherwise %%% issue warning of wrong number of arguments %%% close function
The script to accomplish the task is
%%% Define test conditions x=0:.5:50;y=exp(-.05*x).*cos(2*pi*x/5); figure;F82(x,y); figure;F82(x,y,'r'); figure;F82(x,y,'r','--'); %%% define the function function F82(x,y,c,s) %%% add documentation for the function % calling sequence F82(x,y,[c],[s]) % make a plot given x, y and optional character strings % indicating the line color (c) and style (s). % %%% test for four input arguments if nargin == 4 plot(x,y,[c s]) %%% create line plot with chosen color and style title('Line with color and style control') %%% test for three input arguments else if nargin == 3 %%% create line plot with chosen color plot(x,y,c) title('Line with color control') %%% test for two input arguments elseif nargin == 2 %%% create line plot plot(x,y) title('Line with default style') %%% otherwise else %%% issue warning of wrong number of arguments error('wrong number of input arguments') %%% close function end