5.7: Matlab Functions with No Inputs
- Page ID
- 84406
Tags recommended by the template: article:topic
By Carey A. Smith
Functions can have outputs, even though they have no input arguments.
These are some built-in Matlab functions with no inputs:
date % Try it
datetime % Try it
The function definition of such a file would be like this:
function [date1] = date()
An interesting Matlab function with no inputs is:
pi
This is used as a constant, but it is an actual function. You could define your own functions for constants such as Avogadro’s number, the universal gravitational constant, the speed of light, or a parsec’s distance.
This example is a function (draw to draw a circle or an ellipse.
It has 3 input variables, but no output variable.
A figure should be opened before calling this function.
function [ ] = draw_ellipse(rx, ry, color)
% This function draws an ellipse or a circle
% by drawing short lines between 32 point on a circle
% The inputs are:
% rx = width (x direction)
% height (y direction)
% color: One these: 'b', 'c', 'g', 'k', 'r', or 'y'
% There is no returned value.
dth = pi/16; % (radians) The angle between points
theta = 0: dth : 2*pi;
x1 = rx*cos(theta);
y1 = ry*sin(theta);
plot(x1,y1,color,'LineWidth',4)
end
After calling the function, the user should useaxis
equal
so that the plot has the correct shape.
This script (draw_ellipse_example.m) demonstrates the draw_ellipse function
figure;
rx = 3
ry = 2;
draw_ellipse(rx, ry, 'b')
axis equal
grid on;
title('draw\_ellipse Example')