Practical 3: Arrays
Array Multiplication Expressions
Debugging
The following exercise use code sections, also known as code cells or cell mode in the M-file environment to break code into smaller sections. A code section contains lines of code that you want to evaluate as a group in a MATLAB script, beginning with two comment characters (%%). You can run each section and find the errors independently. Such a procedure is called debugging.
> Use the cell mode environment by following these steps (making sure you update the comment headers first): 1. Open new M-file in the Editor tab. 2. You may like to dock the M-file Editor in the Command Window area by clicking Dock option in Editor window’ Actions button. In this case you can see your code in the Editor window and its execution results below in the Command window. 3. Type the code. |
Cells or sections are marked by the double percent sign- %% and each cell has been given a title. You will notice that when you click on a cell, the text is highlighted in yellow. |
4. Click on the first cell and execute it by pressing the Run Section button. 5. Correct and re-run the first cell. 6. Go through each of the following cells and debug it. You can choose a cell either by clicking on Advance button or simply by clicking the cursor within the cell you want to debug 7. After debugging this exercise, go to Publish tab. Press on Publish button to publish your script M-file as an HTML Document. You will probably find you have some unnecessary output. 8. Close the output window. Add in semi-colons at the end of lines with unwanted output in your M-file before republishing. |
Notes on publishing: Comment lines starting with the double percent %% will be formatted as headers. Additional comment lines will be treated as text. MATLAB commands will be formatted as code. Command Window and graphical output will also be included. For further information, type in the Search for documentation bar ‘Publish MATLAB code’. |