Skip to Main Content Skip to Search
Accelerating the pace of engineering and science

 

MATLAB News & Notes - December 2004

Working with Cell Mode In MATLAB 7

by Ned Gulley

MATLAB sessions often begin with a problem to be solved rather than with a clear plan for writing code. Insight, not code, is the goal.

You might, for example, want to do some quick image processing to count the number of teeth on the clock gear in Figure 1.

How many teeth on the gear?   Figure 1. How many teeth on the gear?

Your session might start at the command line and then move to the editor as the code gains complexity. By the time you reach the solution to your problem, you have a short MATLAB script. This M-file, which you didn’t set out to write, is simply a byproduct of your problem-solving session. In some ways, MATLAB is the programming language for people who don’t plan to write code.

Whether you are problem solving or programming, you may find yourself struggling to choose between the Command Window and the Editor. With the merits of both environments, the new cell mode feature in the MATLAB Editor lets you easily and efficiently refine MATLAB code.

MATLAB scripts often break naturally into discrete sections. Cell mode lets you define separate blocks of code, or “cells,” and then focus on each one separately. These cells enable two important tasks: iterating rapidly on a few lines of code at a time and connecting blocks of code into a meaningful sequence or story. This article explains how to use cell mode to solve our clock-gear image processing problem.

Working at the Prompt

How many teeth are on the gear? We start with a grayscale version of the gear image in a variable called gearGray. We want to find a threshold value that makes the gear stand out as white against a black background. It is difficult to determine the correct threshold value without experimenting first—let’s try a threshold of 80.

gearBW = (gearGray > 80); imshow(gearBW)
Thresholding the grayscale gear image at 80, 100, and 120.   Figure 2. Thresholding the grayscale gear image at 80, 100, and 120.

Image A in Figure 2 shows poor contrast between foreground and background. We need to boost the threshold value.

Using the up-arrow key, we can bring back the last line typed and quickly edit the value. To change 80 to 100, press the up-arrow key, backspace a few times, change the number, and then press Enter to see the result: gearBW = (gearGray > 100); imshow(gearBW)

Editing values in this manner quickly gets tiresome because of the constant repetition of the typing sequence: up-back-back-back-edit-return. It becomes even more cumbersome when you need to run more than one line of code. We could work in the MATLAB Editor, but we would need to give the code a filename and save it as a “temporary” M-file, where it may clutter the hard drive for years. What we really need is a cross between the lightweight immediacy of the command line and the multiline richness of the editor. The answer in MATLAB 7 is the cell mode feature.

In the MATLAB Editor, select Enable Cell Mode from the Cell menu. With cell mode enabled, the editor can execute code directly, without saving a file to disk. Now we can break the code into multiple lines. gearBW = (gearGray > 100);
imshow(gearBW)

To evaluate this code without saving the file, press Ctrl-Enter or click the Evaluate current cell button on the MATLAB Editor toolbar.

Easy Increment/Decrement

When manipulating a number such as the threshold value in our example, we can edit that value directly and press Ctrl-Enter to see the result. For efficiency, we can use the Increment button, shown in Figure 3. We select the value, click + (or press Ctrl-+) and the threshold value increments by 1 and gets evaluated. We quickly learn that 120 is a good cut-off value for this image.

Using Cell Mode in the MATLAB Editor   Figure 3. Using Cell Mode in the MATLAB Editor. Click on image to see enlarged view.

Dividing and Conquering

Working in cell mode, we can divide the code into separate cells for independent evaluation in the current workspace. This approach lets us divide our work into multiple steps. In our example, we first need to load the image from the disk and then set the correct threshold value. As we experiment with the threshold level, there is no need to repeatedly reload the image. Instead, we can separate these two sections of code.

To create a cell divider, we insert a line that begins with two percent characters (%%). This line is generally treated as a comment, but with cell mode enabled, the line instructs the editor to separate one code cell from the next. As a result, MATLAB evaluates only the code in the current cell. Using multiple cells for our M-file helps us focus on the code that needs work while enabling us to tell a story with the code.

Publishing the Story

You can publish any MATLAB script that has been divided into cells. Figure 4 shows how our image processing example looks after it has been published to HTML. We can see the whole story spelled out: threshold the image, clean it up, subtract the hub, and count the teeth. There are 30 teeth, which is not surprising, considering that this gear comes from a clock.

The HTML file published directly from the M-file   Figure 4. The HTML file published directly from the M-file. Click on image to see enlarged view.

In addition to HTML, we can publish to Microsoft Word, Microsoft PowerPoint, or TeX format. The editor provides simple markup commands so that we can add formatted text to the document.

Sharing Code and Ideas

Published M-files are an excellent medium for sharing or archiving code and ideas, because they let the code document itself. The published HTML document is an accurate and easily transferred snapshot of working code, comments, and results. Hundreds of the demos that ship with MathWorks products are now built on this model. In addition, you can post a published file to the File Exchange on MATLAB Central where it can be previewed by others.

See the complete gear example used for this article. To obtain the code from MATLAB, enter

grabcode http://www.mathworks.com/matlabcentral/files/6002/content/html/gear.html

Today, when you are working in MATLAB to gain insight into a problem, the command line and the editor are no longer your only options. The command line is immediate but constrained. The editor groups many lines together but has greater overhead costs. With cell mode, the MATLAB Editor now gives you the best of both worlds.


For more information, visit:


Contact sales
Subscribe to newsletters