Using the cell mode toolbar10

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.

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.

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.

Guy replied on : 1 of 10

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%.

Mike replied on : 2 of 10

Guy, great point. Simulink parameters are an excellent usage of this!

Brett Shoelson replied on : 3 of 10

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
title([algs{incr} ‘ Edge Detection’])

Cheers,
Brett

Mike replied on : 4 of 10

Thanks for that idea, Brett from Pick of the Week. That’s a good use of the incrementer. If my example is the 0-order use, yours is the 1-order. I wonder what the next level is…

béla replied on : 5 of 10

any chance you can get a scrollbar type tool to tweak increments ?
cheers

béla

Mike replied on : 6 of 10

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?

béla replied on : 7 of 10

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.. :)

Mike replied on : 8 of 10

béla,

Not at this time, we’ll take it under advisement. My apologies to your girlfriend.

Petter replied on : 9 of 10

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).

Mike replied on : 10 of 10

@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.