Kahibaro
Discord Login Register

14.4 Using the Editor Debugging Tools

Overview

The MATLAB Editor contains several tools that help you find and fix problems without relying only on the Command Window. In this chapter you focus on what is specific to the Editor debugging tools, how to access them, and how to use them effectively while you work with scripts and functions.

You should already be familiar with basic debugging concepts like breakpoints and stepping. Here you learn how the Editor integrates these concepts and provides extra support while you debug.

Opening Files in the Editor

To use Editor debugging tools you must work in an Editor document, not only in the Command Window. You usually open a file in one of three ways. You can click a .m file in the Current Folder browser, then double click it to open it in the Editor. You can type edit filename in the Command Window to open or create a file. Or you can use the Home tab and click New Script or New > Function to create a new editable file that automatically opens in the Editor.

Once your file is open, the Editor shows line numbers, colored syntax, a left margin for breakpoints, and several debugging buttons on the toolstrip. These are the entry points for almost all Editor debugging tools.

Breakpoint Controls in the Editor

In the Editor, breakpoints are controlled visually. You can click in the grey margin to the left of a code line to create or remove a breakpoint. A red circle appears to indicate an active breakpoint at that line. If the breakpoint is conditional or temporarily disabled the icon changes, so you can see at a glance what will happen when the code runs.

You can right click a breakpoint icon to access context specific options. From there you can disable or enable a breakpoint without deleting it. This can be useful when you want to keep a breakpoint for later but temporarily skip it. You can also set a conditional breakpoint directly from this menu. A dialog lets you type a logical expression that must be true for execution to stop at that line, for example k > 100 or strcmp(mode,"test"). When the code reaches the breakpoint line, MATLAB evaluates this condition and stops only if it is true.

The Editor also supports breakpoints that trigger after a set number of hits. In the breakpoint dialog there is an option to stop after the line has been executed a certain number of times. This is helpful when you are debugging loops where a problem only appears after many iterations. In addition, you can set breakpoints that run a specific command instead of stopping. These advanced breakpoint behaviors are all controlled from the breakpoint context menu or the breakpoint dialog that the Editor provides.

Using the Debug Toolbar

When MATLAB is idle, the Editor toolbar shows common editing controls. Once code is running and you reach a breakpoint, the toolbar changes to show debugging buttons. These are typically labeled Continue, Step, Step In, Step Out, and sometimes Run to Cursor, depending on your MATLAB version.

Continue resumes execution until the next breakpoint or until the program finishes. Step executes only the next line in the current file, then pauses again. Step In goes into the body of a function that is called on the current line. Step Out runs the rest of the current function and returns control to the caller, stopping there. You do not need to remember the command line equivalents of these actions, because the Editor toolbar exposes them as clickable buttons.

The Editor highlights the current execution line with a green arrow in the margin and often a different background color for the line. As you use the Step buttons, this arrow moves, so you can visually track exactly where MATLAB is in your code.

Run to Cursor is another useful Editor specific action. You place the text cursor on a line, then select Run to Cursor from the Debug section of the Editor tab or from the context menu. MATLAB executes from the current breakpoint or line up to the cursor line, then stops there, as if there were a temporary breakpoint at that location.

The Editor’s Current Line and Call Stack View

Besides the green arrow that identifies the current line, the Editor integrates with the call stack that is visible in the Workspace or in specialized debugging panels. When execution is paused, the Editor indicates which file and which line in that file are active. If your code calls other functions, each open Editor tab that is part of the call stack can show its own execution arrow when you select that stack frame.

You can usually open a call stack browser from the Debug section of the Editor or from the command window display when an error occurs. When you click a function name in the call stack, the corresponding file and line open in the Editor, and the green arrow shows the paused line. This tight connection between the call stack and the Editor lets you navigate complex chains of function calls while debugging.

Inspecting Variables with Editor Tools

When execution is paused at a breakpoint, the Editor allows you to inspect variable values directly. If you hover the mouse pointer over a variable name in the code, a small data tip window pops up showing its current value. For simple numeric and string variables the value is shown inline. For arrays and structures you might see the size, type, and possibly a preview of contents. This feature lets you quickly check variable values without typing them in the Command Window.

From a data tip you can often click to see more detail. For example, you can open the Variable Editor for that variable, or navigate into structure fields. The exact options can vary with MATLAB versions, but the essential idea is that the Editor makes it easy to move from code to data view while paused.

In addition, you can select an expression in the Editor when code is paused and right click to evaluate it. The Editor can show you the result of the selected expression in a data tip. This is helpful when you want to test or check a subexpression without modifying the code permanently.

Modifying Code While Paused

A powerful aspect of the Editor debugging tools is the ability to modify code while execution is paused. Once you stop at a breakpoint, you can edit lines in the file, insert new lines or comments, or adjust expressions. After you make changes, you can save the file. In many cases MATLAB can continue execution with these changes, though changes that restructure functions heavily sometimes require you to stop and rerun.

You must be careful when you edit code during debugging, because the currently executing version in memory and the saved file may get out of sync. MATLAB usually detects significant structural changes and may warn you that the code will be reloaded on the next run. The Editor uses colored indicators in the left margin to show which lines have been modified since the last save, so you can keep track of your edits.

Navigating Errors from the Editor

When an error occurs during execution, MATLAB shows a stack trace in the Command Window with clickable links to each location where the error passed through. Clicking one of these links opens the relevant file in the Editor and jumps directly to the line that caused the error. If the file is already open, the Editor simply brings that tab to the front and highlights the line.

Inside the Editor, lines that recently produced errors can also be marked in the margin with markers that are different from breakpoints. You can use these markers to review the area around the error and then place breakpoints nearby for the next run. This workflow allows you to quickly move from an error message to a targeted debugging session using the Editor as the main interface.

Code Analyzer Hints During Debugging

While not a debugger by itself, the Code Analyzer is tightly integrated with the Editor and interacts with debugging. As you debug, the colored bars and messages in the right margin and underlines in the code can hint at possible issues, such as variables that might be unused, code that might never execute, or suspicious expressions.

You can click these colored indicators to see messages explaining the issue. Some suggestions are especially relevant when you are in the middle of debugging an error, because they may reveal mismatched sizes, typographical mistakes in variable names, or unreachable branches. The Editor thus helps you combine dynamic debugging with static analysis.

Finishing a Debugging Session in the Editor

When you are done debugging, you can end the debugging session directly from the Editor toolbar. The Stop button (often labeled Stop or Stop Debugging) clears the current pause state, returns MATLAB to normal operation, and removes the green arrow marker. Your breakpoints remain, unless you explicitly clear them, so you can start another run later without reconfiguring everything.

To clear breakpoints from the Editor, you can right click in the margin and choose Clear All Breakpoints in File. In some MATLAB versions, the Editor also provides a global control to clear all breakpoints in all files from the Debug section on the toolstrip. This lets you reset your environment quickly once you have finished troubleshooting.

Important points to remember:
Use the Editor margin to set, disable, and configure advanced breakpoints, including conditional and hit count breakpoints.
Control execution flow with the Debug toolbar buttons such as Continue, Step, Step In, Step Out, and Run to Cursor.
Inspect variables while paused by hovering for data tips and using the Variable Editor, and evaluate selected expressions directly in the Editor.
Navigate errors and the call stack through clickable links that open files at the exact lines where execution stopped or failed.
End debugging and clear breakpoints from within the Editor so your next runs start from a clean state.

Views: 39

Comments

Please login to add a comment.

Don't have an account? Register now!