Scripts and Functions in Matlab

Matlab → Basics → Statistical Functions → Script File

In command window we are unable to save our work to use it in the future. In this chapter we consider scripts and functions which are saved in files and could be used in the future.Normally, we write a set of commands, called a program.Both scripts and functions are also used to allow user to input data. And, programs in scripts and functions are written in editor window.

Script File
Matlab command window is used for simple arithmetic while script (or script file) is used to write small programs. And, function is normally used to write large and complex programs. Sripts are written in the “Editor window” that could be open using the command “ctrl+n” or by pressing the “Plus button” at left-top corner of the Matlab window. The next Example shows basic script that calculate accumulated amount using continuous compound formula.

This basic programs explains how scripts are written and executed (run). Consider the line 1,

% Continuous Compound Interest Calculation

The % sign shows that it is a comment and not the part of the program. Also note that there is no semicolon at the end of line 5. So that it should show answer when program is executed. If there is a semicolon after line 5 then answer will not be shown.This program is saved using the name “Interest”. And we need to write ‘Interest’ and press ‘Enter key’ to run this program in the command window. In the above example, we assigned values to the variables in the script, while in the the next example, we will assign values in the command window. Again, the program name “Interest” is used to execute this program.