Lab 1. Part 3.

Working in MATLAB

Step 1. Set your current folder/directory

"Current folder" is where MATLAB reads files and writes files. It is important that you set it correctly each time you work in MATLAB. Your current folder is always displayed (see red oval below). In the image below, it is Users/sohielee/Documents/MATLAB


You can change your current folder by clicking on the icons to the left, or by clicking directly on the arrows between folders/files (circled in red below)


The image below shows what happens when you click on an arrow (MATLAB allows you to choose a different folder/file from the available list):


Your task: set your current folder to be your lab1_programs folder

You should see two MATLAB scripts within that folder: lab1.m and buggy.m.

Step 2. Open lab1.m

You can open a file in three different ways:
  1. double-clicking on the lab1.m script listed under Current Folder
  2. clicking on the Open button below Home and selecting lab1.m
  3. In the Command window, typing edit lab1.m
Your MATLAB window should now look like this:

Step 3. Execute lab1.m

You can execute a script in two different ways:
  1. Type the name of the script (without the trailing ".m") in the Command Window:

    Note that the results are shown after typing format compact in the Command Window.

  2. Click on the Run button in MATLAB and select the script to run

Step 4. Make changes to lab1.m

You can type directly in the Editor window and use your mouse to highlight/copy/paste. Make sure to click "Save" when done making changes.

Step 5. One error can suppress all output (an example)

Now, open the file buggy.m and run it. This should generate an error message like this:

Look in the file and see if you can spot the error. Note that even though the error is on Line 9, none of the lines in the buggy.m script produce ANY output due to this error. Fix the error in Line 9. And then run buggy.m again.

Ut-oh. Another error.

Comment out the erroneous line and run buggy.m again. Ah, now it should work correctly.

Take-home lesson: bugs in MATLAB scripts often suppress output entirely, so carefully check your code for errors. Alternatively, you can comment out the buggy line(s) and rerun your code.

You are now ready to start Practice Problems (Lab 1, Part 4).