Lecture 1

Chapter 1 - Introduction to MATLAB

Synopsis
In this class, we will overview the process of submitting homework assignments and discuss Chapter 1: Introduction to MATLAB. The topics that will be covered include the MATLAB environment, variables, assignment statements, numerical and logical expressions, and character encoding.


Submitting Homework Assignments

To keep things uniform between different operating systems, we will use the command-line interface (CLI) to access and interact with the mathcs server. Here are CLI options for your different operating systems:
Windows: Cygwin (be sure to install openssh)
MAC OSX: Terminal
Linux: GNOME Terminal

  1. Open your preferred CLI
  2. Type the following command (replace netid with your Emory netID)

    sftp netid@lab0z.mathcs.emory.edu
    
  3. Enter your password

  4. Use the following commands to navigate through your directory structure:

    pwd   - print working directory - prints your current directory
    ls    - list - provides a list of the files and directories in a specified location
    cd    - change directory - move into another directory
    mkdir - make directory - create a new directory
    rmdir - remove directory - delete an empty directory
    rm    - remove - remove files or directory containing files
    put   - put file - upload a file to the server
    get   - get file - download a file to your computer
    

    All of the commands above perform their operations on the remote server. You can prepend the letter l to these commands (with the exception of put and get) to perform the same operations on your local computer. For example, lcd allows you to change directory on your local computer.

  5. Create a text file on your computer that follows the naming convention outlined on the Assignments page

  6. Add your full name to the text file

  7. Upload your file using the put command


Chapter 1

MATLAB Environment

The MATLAB graphical user interface (GUI) consists of several windows including Current Folder, Command Window, Workspace, and Command History. These windows can be moved around, and may not be in the same location on every computer.

Variable & Assignment Statements

A variable is a storage location with an associated identifier. As in mathematics, variables like x are used to represent some value. We create an association between a variable and a value by using an assignment statement.

1
myvar = 14;

Here, the = is an assignment operator. It does not mean that myvar equals 14. Instead, it means that we are assigning the value 14 to the identifier myvar. There are a lot of fun things we can do with variables, but we can't just name them whatever we want.

Variable Name Rules

  1. Must begin with a letter of the alphabet
  2. Cannot contain spaces
  3. Cannot be longer than namelengthmax
  4. Cannot be a reserved word

Good Practice

  1. Use camelCase
  2. Avoid using underscores _
  3. Use mnemonics
  4. Do not use variables that only differ by case
  5. Should not be a built-in function

Variable Types
Variables come in a variety of flavors. Here are the different variable types in MATLAB:

  1. double (default numerical type)
  2. single
  3. int8, int16, int32, and int64
  4. uint8, uint16, uint32, and uint64
  5. char
  6. logical

Type casting allows us to convert from one variable type to another.

Numerical & Logical Expressions

Assigning values to variables is neat, but the real fun starts when we build expressions. Numerical expressions use values, defined variables, operators, functions, and parentheses to compute some new value or perform an operation.

1
2
a = 5;
b = a + cos(pi)
b =

     4 

Just as in mathematics, MATLAB follows an operator precedence. From highest to lowest, the precedence of the common operators is:

( )      parentheses
^        exponentiation
-        negation
*, /, \  multiplication and division
+, -     addition and subtraction

There are many built-in functions that are useful in numerical expressions. Use help elfun to see a list of elementary math functions.

Relational Expressions
Relational expressions use both relational and logical operators.

Relational Operators
>  greater than
<  less than
>= greater than or equals
<= less than or equals
== equality
~= inequality

Logical Operators
|| or
&& and
~  not

Relational operators relate two expressions of compatible type; whereas, logical operators act specifically on logical operands.

Here is the order or precedence (highest to lowest) including relational and logical operators.

( )                  parentheses
^                    exponentiation
-, ~                 negation
*, /, \              multiplication and division
+, -                 addition and subtraction
<, <=, >, >=, == ,~= relational
&&                   and
||                   or
=                    assignment

Characters and Encoding

All characters in a computer's character set are given a specific integer value. Therefore, we can perform numerical operations on characters and strings. To check the encoding of a particular character, we can use type casting.

1
double('a')
ans =

    97

Final Words

Many of the topics covered in this lecture are basic elements of all programming languages. A solid foundation here will set you up to easily tackle other languages in the future. When you get confused, remember the help function!


Q&A

  • Who should I contact to get MATLAB on my personal computer?
    Please refer to Emory's MATLAB Access page for details on downloading MATLAB.
  • Why does Cygwin say "command not found" when I enter sftp?
    sftp is not loaded by default when installing Cygwin. On the Select Packages installation window, be sure to mark openssh for installation. You can find the package by typing "openssh" into the search box.
  • How do I know if my homework has been collected?
    When homeworks are collected, an entry is added to the log.txt file within your cs130r directory. The log entries will indicate the status of your homework.