Intermediate reference (Matlab)

From robotics


Matlab logo by Mathworks

MATLAB (Matrix Laboratory) is a high level programming language that can perform operations on values, symbols, and matrices. It is commonly used on various technical applications such as signal processing, controls, etc. This tutorial will focus on more advanced skills required in RBE 3001. A basic reference can be found at Introduction (Matlab).

Writing/Reading Serial Port Data

Writing data to a serial port is slightly more complicated than other parts of this tutorial. Begin by setting up an object to represent a serial port using the serial command:

s=serial(PortName, options)

In the serial command, “options” include the baud rate, number of data bits, etc. Next, open the port using the “fopen(object)” command. You will need to specify if the port is being read (‘r’) or written to (‘w’) as an argument. Use fprintf(obj, value) to write value into the serial port. Note: when first writing the code for this, make sure the data is being sent in slowly because MATLAB and serial work at different speeds.

To read data from port PortName, use the scanf command:

V=scanf(obj, ‘format’)

In which the program converts the reading into a ‘format’ type.

Here is an example script] that reads from serial and prints it to the command window.

Debugging Tools

Both scripts and functions can be debugged step-by-step while running. Click on the dash next to a line in the editor to add a break point. Right-click a break point to enable, disable, or set up a condition (Boolean) to stop it there.

Debugging Interface

The editor has various buttons that can be used for debugging:

  • Step
Move on to the next line
  • Step in
Enter the current function and step through its commands
  • Continue
Resume running the program until the next breakpoint, error, or completion of the program
  • Step Out
Step out of the current function being run and into the script or function that is running it
  • Run to cursor
Continue running the program, but treat the cursor’s location as a breakpoint
  • Quit Debugging
cancel running the function or script

Note: If a function or script is running but stops at a breakpoint, the user can type commands into the command window. This is useful for getting information on variables, such as matrices too large for the workspace window to show. However, altering variables in the command window while debugging a program can cause the program to run incorrectly when it resumes.

Sample Function

The function below is designed to plot a real-time clock starting at 12:00. It begins by plotting the circle, then uses trigonometry to draw lines for the second, minute, and hour hands. This should help

  • enter code ISSUES

Try copying/pasting the functions into a single MATLAB text editor (save it as “clockfunc”) and running the clockfunc function with a radius of your choice. A plot will appear with a circle of the radius and each of the hands will have a length equal to that radius. Currently, the center of the clock is (0,0) but changing the startpt vector will shift the center.