matlab basics 4 script files - university of...
TRANSCRIPT
MATLAB Basics 4 script files
Anthony Rossiter
University of Sheffield
1
Slides by Anthony Rossiter
For a neat organisation of all videos and resources
http://controleducation.group.shef.ac.uk/indexwebbook.html
Introduction
1. The previous videos demonstrates how to use basic MATLAB functionality.
2. For more advanced problem solving there is often a requirement to repeat a sequence of computations and also to generate plots.
3. Rather than entering commands over and over again into the command window, it would be easier if we could save a sequence of commands and run them all via a single instruction.
4. MATLAB supports this through script files.
Slides by Anthony Rossiter
2
Creating a script file
• Rather than typing into the command window over and over again, it is better to save the core commands in a file.
• MATLAB provides ‘script files’ for this.
• Select ‘new script’ and save in a folder of your choice.
Slides by Anthony Rossiter
3
Select here.
Slides by Anthony Rossiter
4
Select here to give the file a name and select a folder to save to.
Default ending for MATLAB files is .m
Do not change this!
We will demonstrate the process at the end of this
video.
Advise you set up a folder in advance for any MATLAB
work linked to the same topic and put all your files
in there!
Using a script file
Open the file matlab_basics4a.m
Slides by Anthony Rossiter
5
To run all the code in the file select here. (Alternative is F5)
Got some thing wrong.
Just change the relevant part of the file and run
again! A LIVE DEMONSTRATION
WILL HELP!
Opening existing script files
ALWAYS OPEN FROM WITHIN THE MATLAB ENVIRONMENT.
1) Open MATLAB
2) Open script using the relevant tab within MATLAB (main window or text editor window).
Slides by Anthony Rossiter
6
Useful facts with script files
The code is always executed in strict order of line numbers.
• Instructions must be sequenced carefully – some typical bugs will be illustrated in the following.
You can also run a script file by typing its name in the command window.
• This works only if the file is visible – typical bugs will be illustrated next.
Debugging: MATLAB gives useful hints to help you get the code correct.
Slides by Anthony Rossiter
7
Incorrect sequencing
You cannot use a variable before it has been defined.
Slides by Anthony Rossiter
8
MATLAB provides useful information on the nature of the bug
x is used before it has been defined. Reverse
order of lines 5, 6.
Type name in command window
• In the longer term, opening a script file and using the run button is somewhat cumbersome.
• Easier just to type the name.
Slides by Anthony Rossiter
9
This only works if the file is in the same folder as MATLAB
Check your files using the current folder tab.
File not found
If you try to run a file which is not in the current directory, you will get an error message.
Slides by Anthony Rossiter
10
Wrong folder so file not found
Correct folder so file now runs without error.
REMARK: More advanced users can create search paths, but I
would advise against this for low level basic users as it can create
confusion.
Finding bugs
Putting code sequences in files and running via a file name is a good mechanism for locating bugs.
Here, MATLAB has identified precisely which line has a bug and also the nature of the bug.
Slides by Anthony Rossiter
11
REMARK: x and w are clearly different lengths
Further advantages of script files
Once a script file exists, you can run it from within another script file just by typing the name.
Thus you can build a portfolio of useful files for common tasks.
Slides by Anthony Rossiter
12
First run existing file to generate source data.
This file focusses on making the plot look nice.
LIVE DEMONSTRATIONS WITH MATLAB
Slides by Anthony Rossiter
13
Run the following
matlab_basics4a.m
matlab_basics4b.m
matlab_basics4c.m
matlab_basics4d.m
matlab_basics4e.m
Conclusions Demonstrated the usefulness of MATLAB for script files for executing sequences of commands in an efficient manner. 1. Allows complex computations to be tested and
saved for easy use later. 2. Allows efficient modification, e.g. different
functions, domains, colours, etc. . 3. Allows user to create a portfolio of re-useable
scripts for regularly used tasks that can be called from other scripts.
REMARK: More advanced users will tend to use function files not script files as scripts work directly on variables in the workspace and thus can interfere with each other if used carelessly.
Slides by Anthony Rossiter
14
© 2016 University of Sheffield This work is licensed under the Creative Commons Attribution 2.0 UK: England & Wales Licence. To view a copy of this licence, visit http://creativecommons.org/licenses/by/2.0/uk/ or send a letter to: Creative Commons, 171 Second Street, Suite 300, San Francisco, California 94105, USA. It should be noted that some of the materials contained within this resource are subject to third party rights and any copyright notices must remain with these materials in the event of reuse or repurposing. If there are third party images within the resource please do not remove or alter any of the copyright notices or website details shown below the image. (Please list details of the third party rights contained within this work. If you include your institutions logo on the cover please include reference to the fact that it is a trade mark and all copyright in that image is reserved.)
Anthony Rossiter Department of Automatic Control and
Systems Engineering University of Sheffield www.shef.ac.uk/acse
For a neat organisation of all videos and resources
http://controleducation.group.shef.ac.uk/indexwebbook.html
Addendum: Why not script files?
Good for testing code snippets and ideas, however:
1. All variables are in the command window workspace.
2. Once you have 50-100 files, you will lose track of which variables different files use.
3. Files will corrupt the variables used by others and thus work unpredictably.
In the long term, you strongly advised to use function files for final code even if script files are used in a drafting and testing stage.