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:
- A MATLAB statement
- A Comment
- White space
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.
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.
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.
Download the script template to be used for all scripts written in this class for a grade.
- The scripts you write for homework assignments must use this template.
- Submitted scripts should emit only to the command window outputs specified in the assignment.
- Submitted scripts should not stop, pause, or require user input unless specified in the exercise.
- The composition of the supplied source files are graded for the use of comments, white space and MATLAB statements; all are equally important.
- The script must run to completion with no syntax errors and correctly produce the specified results.
%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
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.
- The name of the script
- A description of the purpose and computational result of the script
- The syntax for using the script
- The inputs or variables that must exist in the global Workspace and the values they must contain. (The Domain of the function)
- The outputs of the script (The Codomain of the function)
- Other files used by the script that must be in the path of the MATLAB IDE so that they can be found and executed.
- A list of other related scripts or functions
- The name (and student id) of the author and the author's contact information
The header may include
- a copyright (or copyleft) notice.
- company information
- date/time stamp
- 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.
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
- The Header contains the description of the script and will be displayed in the command window with the help MATLAB command.
- The Body contains the MATLAB statements, comments and white space that make up the implementation of the algorithm.
- 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.
- 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 .
- 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,
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 .
Write a script named
HelloAll
. This script should be an ASCII file name
HelloAll.m
. It should contain
HelloAll.m
HelloAll should perform the following steps:
input
to emit the string
"What is your name? "
to the MATLAB command window, and
name
'\n\nHello Everyone. My Name is %s. How are you?\n\n'
%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 Attribution-NonCommercial-ShareAlike 4.0
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.
Noncommercial
You may not use the material for commercial purposes.
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.