# 5.8: Matlab Functions with No Outputs

• • Carey Smith
• Oxnard College
$$\newcommand{\vecs}{\overset { \rightharpoonup} {\mathbf{#1}} }$$ $$\newcommand{\vecd}{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$$$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\kernel}{\mathrm{null}\,}$$ $$\newcommand{\range}{\mathrm{range}\,}$$ $$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$ $$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}{\| #1 \|}$$ $$\newcommand{\inner}{\langle #1, #2 \rangle}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\kernel}{\mathrm{null}\,}$$ $$\newcommand{\range}{\mathrm{range}\,}$$ $$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$ $$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}{\| #1 \|}$$ $$\newcommand{\inner}{\langle #1, #2 \rangle}$$ $$\newcommand{\Span}{\mathrm{span}}$$$$\newcommand{\AA}{\unicode[.8,0]{x212B}}$$

Tags recommended by the template: article:topic

By Carey A. Smith

A MATLAB function can have no output variable, although it may display something or do some action. An example is:

clear all -- clears all variables

The user-defined function below draws a star, but does not return a value. Note that in the function declaration line (signature) that empty square brackets are placed where the output value would normally go. The inputs are the size and color of the star.

##### Example $$\PageIndex{1}$$ Function with no outputs: starxy()

function [] = starxy(r, color)

% Draw a star of radius r

% color is a character, such as 'r'

dth = (4*pi/5); % delta theta

theta = (pi/2) + [1:1:6]*dth;

x = r*cos(theta);

y = r*sin(theta);

figure;

plot(x, y);

axis equal;

end

Test of starxy():

starxy(2, 'g') % This draws the green star shown in the figure. Figure $$\PageIndex{i}$$: Function with no outputs: starxy() draws a star.

Solution

MATLAB and Octave also allow the []= to be omitted from the function declaration when there is no return value. So this declaration also works:

function starxy(r, color)

.

##### Exercise $$\PageIndex{1}$$ Rectangle Draw

Part A. Write a function rectangle_draw_YourName

% Function's 1st line is:

function [ ] = rectangle_draw_YourName(width, height, color)

% It has 3 input variables (a.k.a. arguments): % width (x direction)

% height (y direction)

% color: Can be any one these: 'b', 'c', ''g', 'k', 'r', or 'y'

% The [ ] in the function's 1st line indicates that it has no output variable. It draws a figure instead of computing an output value.

Inside the function, open a figure and turn on hold.

figure; hold on;

Then draw 4 line segments to create a rectangle. Think of the base paths on a baseball field.
The 1st line segment is like going from home plate to 1st base.

This line is horizontal, so the y coordinates of both points are the same.

% Home to 1st base: x1 = [0, width]; y1 = [0, 0]; plot(x1,y1,color,'LineWidth',10)

The 2nd line segment is like going from 1st to 2nd base.

This line is vertical, so the x coordinates of both points are the same.

%% 1st base to 2nd base: x2 = [width, width]; y2 = [0, height]; plot(x2,y2,color,'LineWidth',10)

The 3rd line segment is like going from 2nd to 3rd base. Try to figure this out.

The 4th line segment is like going from 3rd base to home plate. Try to figure this out.

Part B. Write a test file that tests your function with these inputs:

width = 40;

height = 30;

color = your choice of 'b', 'c', 'g', 'k', 'r', or 'y'

rectangle_draw_YourName(width, height, color)

function [ ] = rectangle_draw(width, height, color)
%% The code opens a figure and draws a rectangle
% The inputs are:
% width (x direction)
% height (y direction)
% color: One these: 'b', 'c', 'g', 'k', 'r', or 'y'
% There is no returned value.
figure;
hold on;

%% It will draw 4 line segments.
% Think of these as the lines between the 4 bases in baseball.
% For each line segment, specify a vector of the first and last x coordinates
% and a vector of the first and last y coordinates.

% Note that the x and y vectors have only 2 values.

%% Home to 1st base:
x1 = [0, width];
y1 = [0, 0];
plot(x1,y1,color,'LineWidth',10)

%% 1st base to 2nd base:
x2 = [width, width];
y2 = [0, height];
plot(x2,y2,color,'LineWidth',10)

%% 2nd base to 3rd base:
x3 = [width, 0];
y3 = [height, height];
plot(x3,y3,color,'LineWidth',10)

%% 3rd base to home
x4 = [0, 0];
y4 = [height, 0];
plot(x4,y4,color,'LineWidth',10)

%% % Set the x and y limits bigger than the rectangle
xlim([-0.1*width, 1.1*width])
ylim([-0.1*height, 1.1*height])

.

##### Exercise $$\PageIndex{1}$$ Trinagle Draw

You can use refer to the rectangle_draw.m function as a pattern for this assignment.

Assignment:

Write a function triangle_draw_YourName

The code opens a figure and draws a right triangle

% The inputs are:
% width
% height

% Option: specify the color as a 3rd input. This is not required.

% The function draws 3 line segments.

% The 1st line segment is horizontal. x goes from 0 to width; y is 0.

% The 2nd line segment is vertical. It goes up by height from the end of the 1st segment.

% The 3rd line segment goes from the end of the 2nd segment back to the strating point (0, 0). This line is diagonal.

% Write a test file that tests your function with these inputs:

width = 40;

height = 30;