Wichita State University Logo

Math 451: Computational Mathematics

1.6 Writing Your First Script


1.6.1 What is a Script?

A script is a text file with a list of MATLAB statements. The file extension of a MATLAB script is ".m". The file name of our first example is "hello.m". Although file extensions are not extensively used on some operating systems, we need them when working with MATLAB. The MATLAB interpreter parses a script file just like it parses the command window. It parses a single line of the MATLAB script file and executes the statement. MATLAB scripts are text files with three kinds of text in each line of the file:

  1. A MATLAB statement
  2. A Comment
  3. White space
  1. A MATLAB statement can be any MATLAB statement one would type into the command windows; that is any syntactically correct statement in the MATLAB language.

  2. A Comment begins with the "%" symbol and everything on the line of text after the "%" symbol is ignored by the MATLAB interpreter. Comments are an important part of the script. It is an integral piece of the composition that makes up a script or function. Comments are read by other humans; programmers, testers, technical support personnel and reviewers that read the source of the script. Comments provide information about the conditions necessary for a script or function to obtain the correct result. Comments provide the update history of the script and the contact information of the authors. Comments clarify the intent of the computational programmer in a human-readable form that may not be clearly explained by the MATLAB source code. Many times comments are for the future author of the script. Programming languages are terse, and a script must be written to communicate the goals and computational details of the algorithm to even the original author.

  3. White space consists of spaces, tabs or line breaks. White space is ignored by the MATLAB interpreter. White space is used to make the composition of the script or function easy to read. We add white space to a script or program to lead the eye of the reader to the logical or semantic structure of the source code.

1.6.2 The Script Template

Download the script template to be used for all scripts written in this class for a grade.

  1. The scripts you write for homework assignments must use this template.

  2. Submitted scripts should emit only to the command window outputs specified in the assignment.

  3. Submitted scripts should not stop, pause, or require user input unless specified in the exercise.

  4. The composition of the supplied source files are graded for the use of comments, white space and MATLAB statements; all are equally important.

  5. The script must run to completion with no syntax errors and correctly produce the specified results.

1.6.3 Template_Script_Header.m

%Homework1 - 
%
% Syntax:  Homework1
%
% Inputs: none
%
% Outputs: displayed results of problems
%
% Other m-files required: none
% Subfunctions: none
% MAT-files required: none
%
% See also: OTHER_FUNCTION_NAME1,  OTHER_FUNCTION_NAME2
%
% Author: FirstName FamilyName
% Math 451 Assignment HW 1
% email: 
% Website: https://blackboard.wichita.edu/
% 22-Jan-2019; Last revision: 22-Jan-2019
%
%------------- BEGIN CODE --------------  

% Exercise 1  
% Create a 10x10 Array of integers between 0 and 50  

A = randi([0 50], 10, 10)  

% Exercise 2  
%------------- END OF CODE --------------
%Please send suggestions for improvement of the above template header 
%to Denis Gilbert at this email address: gilbertd@dfo-mpo.gc.ca.
%Your contribution towards improving this template will be acknowledged in
%the "Changes" section of the TEMPLATE_HEADER web page on the Matlab
%Central File Exchange

1.6.4 The Script Header

Notice that the template demonstrates a MATLAB statement, the use of comments and the use of white space. There is a blank line between the end of the header ("%------------- BEGIN CODE --------------") and the first comment or executable statement. The use of white space helps the reader understand the structure of the script. The blank lines allow the reader to identity blocks of comments and MATLAB statements. The comments identify important elements of the data and algorithm implemented by the script for the reader. Comments explain the use of the MATLAB statements. One can compare the MATLAB statement to the author's description to verify the programmer's intent against the implementation.

If one types into the MATLAB command line

>> help template_script_header

the script_header is displayed in the command window. The software developer uses the script header to communicate important information about the script to a user.

Figure 1


The header should always include

  1. The name of the script
  2. A description of the purpose and computational result of the script
  3. The syntax for using the script
  4. The inputs or variables that must exist in the global Workspace and the values they must contain. (The Domain of the function)
  5. The outputs of the script (The Codomain of the function)
  6. Other files used by the script that must be in the path of the MATLAB IDE so that they can be found and executed.
  7. A list of other related scripts or functions
  8. The name (and student id) of the author and the author's contact information

The header may include

  1. a copyright (or copyleft) notice.
  2. company information
  3. date/time stamp
  4. A revision history of the script. A list of the dates the script was edited and a description of any changes that were made.

The last cell in this page contains the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 copyleft notice.

1.6.5 Hello

Our first script should be simple and demonstrate our three primary objectives. In this script we want to prompt a user for their name and display a friendly message in the command window.

%Hello 
% Hello asks for the users name and displays a friendly message
%
% Syntax:  HELLO
%
% Inputs: none
%
% Outputs: displayed results of problems
%
% Other m-files required: none
% Subfunctions: none
% MAT-files required: none
%
% See also: input, fprintf
%
% Author: FirstName FamilyName
% Math 451 Assignment Example 1
% email: 
% Website: https://blackboard.wichita.edu/
% 17-Aug-2020; Last revision: 17-Aug-2020
%
%------------- BEGIN CODE --------------

name = input('Hello, what is your name? ');

fprintf('\n\nHello %s, how are you?', name)
fprintf('\n\nComputers are fun!\n\n')

%------------- END OF CODE --------------
%Please send suggestions for improvement of the above template header 
%to Denis Gilbert at this email address: gilbertd@dfo-mpo.gc.ca.
%Your contribution towards improving this template will be acknowledged in
%the "Changes" section of the TEMPLATE_HEADER web page on the Matlab
%Central File Exchange

The file "Hello.m" is structurally divided into several parts using of white space and comments. The top level structures of the source file are the

  1. The Header contains the description of the script and will be displayed in the command window with the help MATLAB command.

  2. The Body contains the MATLAB statements, comments and white space that make up the implementation of the algorithm.

  3. The Trailer contains comments about the script or the organizational information of the script. The trailer can contain notes about the script that one does not want displayed with the help command. This can include a list future features or work still to be done. This can include the script's role in a larger software package or company information. The current template contains the original author's contact information.

The body of our script consists of two organization blocks separated by white space.

  1. An input block that consists of the MATLAB statement that displays a prompt and collects typed input from the user. A prompt a string displayed in the MATLAB command window that indicates to the user that input is requested. The MATLAB function input displays the prompt string in the command windows, collects the user input and returns the input. The MATLAB statement assigns the input supplied by the user to the variable name .

  2. The output block consists of two MATLAB statements that display a friendly message to the user. The fprintf MATLAB function displays formatted strings to the command window. The name fprintf is an acronym for file print formatted . It requires a format string that describes how the subsequent arguments to the function should be displayed. It is a powerful function that performs many of the features of the identically named function in the C programming language and the FORTRAN function write .

If one invokes the hello script in the command window,

Figure 2

Notice that to respond with the string , "William", the user was required to enclose the name is single quotes to indicate to the input function that the response was a string.

1.6.6 The Script Trailer

A copyright notice, organization specific information, and miscellaneous information may be contained in either the header or trailer. I am aware of no formal specification of a source code trailer. After this course you will need to write your own code specification or coding convention that defines what you want in your source code trailer information. Before writing any programs for an employer or client make sure to read their code specification if they have one.

In this class we use the contact information of the original author of our source code template .

16.6.7 Your First Script

Write a script named HelloAll . This script should be an ASCII file name HelloAll.m . It should contain

  • the script template
  • modify the script template to match your new script HelloAll.m
  • modify the script template with your student Id, your name, and the date you write the script

HelloAll should perform the following steps:

  1. Utilize the MATLAB builtin function input to emit the string "What is your name? " to the MATLAB command window, and
  2. assign the user response to the label name
  3. use formatted printing to emit the string '\n\nHello Everyone. My Name is %s. How are you?\n\n'
  4. the format specifier %s should emit your name at the appropriate place in the string.

When your grader executes your script the output to the MATLAB command window should be,

>> HelloAll
Hello, what is your name? 'William'


Hello Everyone. My Name is William. How are you?

>>

Creative Commons Logo - White


Your use of this self-initiated mediated course material is subject to our Creative Commons License .


Creative Commons Attribution-NonCommercial-ShareAlike 4.0

Creative Commons Logo - Black
Attribution
You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.

Creative Commons Logo - Black
Noncommercial
You may not use the material for commercial purposes.

Creative Commons Logo - Black
Share Alike
You are free to share, copy and redistribute the material in any medium or format. If you adapt, remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.