MATLAB Spoken Here

Using the cell mode toolbar 10

Posted by Michael Katz,

The MATLAB Editor is great because supports many different workflows and user styles. Even though I call myself an advanced MATLAB user, I have long since stopped being surprised when I find out about something that has been there forever and is quite blatant in the UI. There are just some things that I don’t need to use in my normal work, but may be quite useful to others. One such thing is the cell mode toolbar. If the toolbar is not already present in the editor, you can activate it from the menu: Cell -> Enable Cell Mode.

cell mode toolbar

The first two items on the toolbar are the ones I use a lot. They are evaluate current cell (Ctrl+Enter) and evaluate cell and advance to next (Ctrl+Shift+Enter). This is great for live demos, where you can put a single concept (of multiple commands) in a cell and then step through a file one cell at a time. The demo workflow also fits nicely with publishing, where each step of the demo publishes into a separate section. Just remember that evaluating a cell evaluates the enclosed commands in the base workspace, which is not necessarily the same workspace as if the file were run regularly.

The next two sections of the toolbar have a textbox surrounded by either a “+,-” or a “x,%”. It’s not obvious what to do with these. If you place the caret in the editor next to a number or select a number, then the +,-,x,% buttons will light up. Pressing them will increment/decrement or multiply/divide the selected number by the amount in the box and reevaluate the current cell. This is good for tweaking a numerical parameter, such as a coefficient, tolerance, or number of iterations/interpolations. In this example I’m tweaking the peaks input, which produces a nice visual change on each increment.

cell mode toolbar

The next two buttons %% are a little more obvious but their icons are subtly different. The first one has a plus “+” in the icon and clicking it inserts a new cell marker. It’s handy to have that for the mouse, but I find it faster to type “%%”. The second one provides you a dropdown list of the cells in your file, the same way the function dropdown provides a list of functions in the file.

And remember, you can always customize the buttons that show up on the cell mode toolbar: File -> Preferences -> Toolbars -> Editor Cell Mode.

10 CommentsOldest to Newest

My favorite element of the cell mode toolbar is the “-+” part. Since I discovered the possibilities of this feature, I use it everytime I need to tune or debug a Simulink model.

By writing a simple script, I can change the value of a parameter (using the “-+ feature”), simulate the model using SIM, plot the results and see the effect. click, click, click, done. It speeds up things by 1000%.

Hey Ken and Mike,
Whenever I present MATLAB in bootcamp mode (which is very often), I always highlight the utility of cell-mode editing. One of the benefits I point out is the vastly under-utilized incrementer tools–few people know what they’re for. I typically will increment an initial guess at a threshold for an image, for instance.

But I also like to point out that one can get more creative with these buttons. For instance, if one wanted to try out our 6 different (IPT) algorithms for edge detection, the incrementer tools allow that, too:

%%
algs = {‘Sobel’,’Prewitt’,’Roberts’,’LOG’,’Zerocross’,’Canny’};
incr = 1; %INCREMENT HERE
imshow(edge(tissueMask,algs{incr}))
title([algs{incr} ‘ Edge Detection’])

Cheers,
Brett

béla,

I’m not sure how a scrollbar would help because (a) we’d more space, and (b) the limits are unknown (scrollbars need a minimum and maximum value. How do you envision using a scrollbar for the increments?

hm.. got your point.. not sure..
but for example is it somehow possible to use the mouse wheel to change the values ?
It is just a bit more natural to play with the wheel, and besides.. it annoys the hell out of my girlfriend if I am clicking a lot.. :)

Do you know how to execute a cell from the command window (or from another script)? I currently have like 20 scripts in a file using cell mode (I do not want 20 different files), and would like to generate a script that runs several (but not all) of them after each others (so I can take a cup of coffee).

@Petter,
Cell mode really isn’t meant to be used this way. It’s really more for markup and does not have any meaning in the language.

My first suggestion would be to make each of these individual logical units, such as separate functions (which you don’t want), or to make your main file a function and each cell as subfunction, then call the main function and pass in the subfunctions you want to runs, and selectively execute each subfunction. You can have the zero-input case run all of them.

Or you could guard each cell with a condition and set all the appropriate conditions before running the file.

Finally you can play text games. For instance, get the text and use EVAL to run a sub-set. Obviously this is fragile depending on the existence of “%%’s” that aren’t cell markers

a = matlab.desktop.editor.getActive
cells = regexp(a.Text,'%%.+?((?=%%)|(?=$))','match')
eval(cells{1})

If you want to generate a new script with some subset of the original’s cells:

n = matlab.desktop.editor.newDocument
n.Text = [cells{1} cells{3}]

These postings are the author's and don't necessarily represent the opinions of MathWorks.