MATLAB Basics 5 variable types - University of...
Transcript of MATLAB Basics 5 variable types - University of...
MATLAB Basics 5 variable types
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. It is useful to consider the range of mathematical and other problems that can be handled efficiently.
3. To do so, a number of different variable types are needed – a summary of the main ones supported by MATLAB are given here.
Slides by Anthony Rossiter
2
The intention is to give you exposure to the most important variable types in basic use, not a comprehensive list.
List of variable types
• Numerical values (or numbers) and integers.
• Symbolic variables (no numerical values implied).
• Complex numbers.
• Vectors and Matrices (2D and nD).
• Logic variables (true and false).
• Strings (for storing textual expressions).
• Mathematical functions and expressions.
• Laplace transforms (see resources on control).
• Cells and structures
• Etc.
Slides by Anthony Rossiter
3
Numerical values
The most common variable type, in essence the default is a matrix.
This includes vectors and scalars.
Matrix denotes these with the classification of “double”.
Slides by Anthony Rossiter
4
We will not cover n-dimensional arrays here.
Symbolic variables Symbolic variables do not contain values and allow the user to write algebraic expressions.
This supports operations such as differentiation.
Matrix denotes these with the classification of “sym”.
Slides by Anthony Rossiter
5
Command to create symbolic variables
Complex numbers
The default value in a matrix is always a complex number.
Hence complex numbers are also captured in the classification of “double”.
Slides by Anthony Rossiter
6
Logical variables
Logical variables are useful tools as these are often used in decision making.
MATLAB has a special class for this of “logical”.
Slides by Anthony Rossiter
7
Command to create a logical variables
Supporting decision making.
More examples in demonstration at end of this video.
Strings Sometimes we want text, for titles and labels and also where this is more expressive than a number.
Matrix denotes “text” with the classification of “char”.
Slides by Anthony Rossiter
8
Command to create a character array
Notice, num3 is a double!
Structures A student will often store multiple resources in a single folder. In a similar way, MATLAB allows you to store multiple related variables under a single variable name, thus making saving and transfer of this information more efficient. Matrix denotes these with the classification of “structure”. (See ‘help struct’ for more)
Slides by Anthony Rossiter
9
Store all data in a
single variable
‘ALL’
Extracting data shown in live demonstration
Cells These are conceptually equivalent to structure arrays, but information stored by index position rather than name.
Matrix denotes these with the classification of “cell”.
See ‘help cell’ for more.
Slides by Anthony Rossiter
10
Notice use of brace brackets for
accessing different positions.
Anonymous functions
A useful shorthand to define mathematical functions of the type y=f(x). Without creating a separate m-file.
Matrix denotes these with the classification of “function_handle”.
Slides by Anthony Rossiter
11
Can be used in many other MATLAB function files.
Notice use of @(x) to define function and independent
variable.
LIVE DEMONSTRATIONS WITH MATLAB
Slides by Anthony Rossiter
12
Go through the following to see core variable types and their creation
matlab_basics5.m
Conclusions
Demonstrated the variety of variable types available in MATLAB to support different types of computation and decision making.
1. This resource is a very quick overview and by now means comprehensive.
2. If we have not covered what you need, use the MATLAB help and no doubt it is there somewhere.
REMARK: You will understand these variable types and uses best by active use.
Slides by Anthony Rossiter
13
© 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