Ken & Mike on the MATLAB Desktop

January 7th, 2008

I’ve got something to cell you

The MATLAB Editor is similar to other editor’s in that it’s a great tool with which to develop software. Additionally, though, the MATLAB Editor can be used as an investigative and exploratory tool, a publishing tool, and a demonstration tool. The feature that makes all this possible is Cell Mode.

Cell mode allows you to mark up your M-file with cell dividers (%% is the cell divider token). Each cell represents a logical block of code that can be run relatively independently of the rest of the code. I say relatively because a cell may use variables in the workspace including those defined in a previous cell.

Here’s what a file using cell mode looks like (the file below is [matlab root]/toolbox/matlab/demos/imagedemo.m - you can view its published output here):

The MATLAB Editor’s cell mode is a great tool for investigation and data exploration, as well as MATLAB publishing (I’ll cover these two topics in later entries), but I find it most valuable for giving great demos. Cell mode lets you have MATLAB running, with the code your going to demonstrate up on the screen. You can easily describe what a given cell does, as it is right up in front of your audience and it’s shown in context with surrounding code. Then with a simple keystroke (โŒ˜โ‡งโŽ on Mac and Ctrl-Shift-Enter on Windows) you can evaluate that cell. I find that showing and explaining the code before displaying its output helps viewers understand what they are seeing. Cell mode provides a very natural flow between cause and effect. We use cell mode here at The Mathworks for all our MATLAB demos.

Heres how I would demo [matlab root]/toolbox/matlab/demos/imagedemo.m:

[MATLAB Ken enters stage right]…

And there you have it! An interactive demo, which lets you step through your code as you apply transforms to your data. Cell mode lets you focus on and interact with your audience. You don’t have to type code as you go to get progressive disclosure - you need only select a cell and the MATLAB Editor will help focus your audiences attention on that cell by highlighting it. Your audience gets the benefit of seeing the actual code, and then, right before them, they can watch as it generates output.

For more info, check out the cell mode help which will have you using cells in no time!

Do you use cell mode?

26 Responses to “I’ve got something to cell you”

  1. Dan Kominsky replied on :

    I do use cell mode a lot, but there are two things which really bug me about it.
    1. Breakpoints and the like (including dbstop if error) don’t work, and
    2. The %% Has to be in columns 1&2 which doesn’t jive with the smart indenting functions.

    Any thoughts on when these two issues might be resolved?

    Thanks,
    Dan

  2. Ken replied on :

    Hi Dan,

    Glad to hear you are using cell mode!

    Re. 1: The ability to stop at breakpoints has come up before. We’re still undecided as to whether or not to include this feature. Some people publish as the final step in their work flow (so debugging would be done in a previous stage), but theres also another camp that would like a more interactive work flow. We’d love to hear more about how you work so that we can reevaluate previous assumptions.

    Re. 2: This limitation is currently being addressed and will be removed very soon! Stay tuned…I’ll reply to this comment when the feature goes live.

    Thanks for your feedback Dan,
    -Ken

  3. Dan Kominsky replied on :

    I mostly use the cell mode for algorithm development. When I want the program to run at high speed I generally incorporate it into a function so it can benefit from the JIT. The main benefits of using cell mode for algorithm development are that it assigns the variables into the base workspace so I can use the interactive tools to keep track of values, plot intermediate results, etc. Also I can work on just a small portion of my code without having to do what may be very extensive initialization routines over and over. I guess that your question is whether cell mode serves any function outside of publishing, and I assure that from my perspective is certainly does! I have never yet published a single m-file, but I am often using cell mode, and not being able to know what line bombed is a real headache for me.

    Thanks,
    Dan

  4. turtie replied on :

    I also use cell mode and find it extremely helpful to separate code into blocks. I usually use this when programming GUIs, which helps me organize all the callback and create functions.

  5. Ken replied on :

    Hi Dan,

    I had a brain cramp in my last response - I was reading cell mode but thinking publishing! Your absolutely right about the need to debug in cell mode. We’ve also had requests to supply the line number when the executing cell fails (a modest but very useful bit of information). We’d really like to make cell mode as powerful and friendly a feature as possible.

    I typically envision three major uses of cell mode: demonstrations (as blogged about above), publishing (as was so blindingly stuck in my head earlier!) and investigation (as you pointed out). We’ll definitely talk more in depth about cell mode as a publishing and investigation tool in future entries.

    Thanks for your feedback Dan,
    -Ken

  6. Ken replied on :

    Glad to hear it Quan! I’m interested in hearing more about how you use it in GUI development. Do you play around with a cell until you get the right behavior, and then break that out into a function as Dan suggested?

    Thanks,
    -Ken

  7. Oliver A. Chapman, P.E. replied on :

    I’m still trying to effectively use the cell mode. It seems like a good idea and I’ve revised a ~1500 line, 11 function .m file project to include cells. But, it didn’t seem to significantly enhance the readability of the code like I expected. About half of the above line count are comments. Good coding style calls for appropriate use of functions and it seems that cells don’t contribute significantly more than the use of functions.

    However, this discussion of using cells for demonstrations is a different spin and I look forward to your discussion of cells in publication.

  8. Ken replied on :

    Hi Oliver,

    I agree that cell mode won’t enhance the readability of a previously function based file (other than adding some visual delineations between logical code blocks). In fact, I’ve heard from a number of developers that actually work in the reverse order that you mentioned. That is, they first develop their code using cells, tweak it until the cell behaves as they desire, then they pull that cell out into a function.

    In your particular case, I might suggest breaking your single file into multiple files if possible. For example, if you have setup related cells, you might put those in setup.m. You could then run each ’setup’ cell verifying along the way that your environment looks correct. Alternatively, you could run the file as a whole, but you’d retain the ability to step through each cell if problems arose.

    Also, if you’ve broken your code into multiple files, chances are the logic (rather than the less interesting setup) will live it’s own file(s). This will allow you to publish only your logic, even if your logic file can’t be run by itself. I’ll show how to use publishing in this way in a future post.

    Thanks for your feedback Oliver!
    -Ken

  9. Brian replied on :

    I have been using cell mode to create tutorials for a course on scientific computing. I really like the ability to create one document and configure the publish options to create different versions with just documentation (e.g. questions), or show the code, or evaluate the code. This is a great addition to MATLAB!

    I had two questions:
    1) Is there a way to publish a whole series of documents? For example, to create a set of linked HTML files, where function calls in a script link to the published version of that function?

    2) Is there a way to modify the options structure while a file is being published? For example, turn off evaluation of code for a while, and then turn it back on?

    Thanks,
    Brian

  10. Ken replied on :

    Hi Brian,

    Glad to hear you’re finding cell mode useful! We always enjoy hearing how people are putting features to use - it certainly helps inform the evolution of a feature.

    Re. 1: There is currently no way to publish a series of documents, though similar requests have been made. I’ll take note of your comment and make sure it’s added to the original enhancement request.

    Re. 2: There’s currently no way to easily change the publish settings on a per-file basis, but there is a new feature called publish configurations in the pipeline (stay tuned here and I’ll reply to this comment when the feature is shipped), which will do exactly what you want!. The feature is similar to run configurations, which were new in R2007b. I encourage you to check run configurations out, as it’ll give you a leg up on understanding how to use publish configurations.

    -Ken

  11. kristin replied on :

    Excellent blog entry Ken!

  12. Ken replied on :

    Thanks Kristin!

  13. Scott replied on :

    Just to add to Ken’s response to Brian’s question. I’m not sure if this would help, but you can use the PUBLISH command to publish programmatically. This might make it easier to publish a whole bunch of m-files at once, and also allows you to specify the publishing options for the current file.

  14. Ken replied on :

    Great point Scott. You can find the publish function help here.

    A script that took a list of files or a directory and a set of publish options and then called publish on all those files would be a great addition to MATLAB central…any takers!?

    -Ken

  15. Ken replied on :

    @Dan Kominsky (comment 2533): R2008a has been released! In this version, you can now have %% on any column within the line - the restriction forcing this token to columns 1 & 2 has been removed.

    Check out the full release notes here.

    -Ken

  16. Ken replied on :

    @Brian (comment 2631): R2008a has been released! There’s a shiny new feature called Publish Configurations that allows you to specify publish options on a per-file basis. I’ll be blogging about this feature soon.

    Check out the full release notes here.

    -Ken

  17. Sven replied on :

    A little late in my response, but I’ll go on…

    I agree wholeheartedly with Dan Kominsky. I also use cell mode during algorithm development. It’s very useful for zooming in on a chunk of code and, as Ken points out, also useful for teasing out a separable chunk that can become a function.

    The whole point here though, is that this is exactly the environment in which breakpoints would be useful. Particularly when your m-file is a script, I find it hard to break execution on the line I want. I often go to the lengths of selecting all the previous text, commenting it out, and then running the code with f5 and my breakpoints on. (and then uncommenting when I’m done).

    I have to admit that I’m not (yet) using publish so I may be missing something here, but I’m not sure how having breakbpoints disabled in cell mode is useful to the user.

    Thanks,
    Sven.

  18. Alexander Kemp replied on :

    I definitly like cell mode for publishing. The major point which is missing for me is programmatic access to the currently evaluated cell/to the list of cells in a m-file. Maybe i am blind, but i did not find it in the manual.

  19. Ken replied on :

    Hi Alexander,

    Glad to hear you like publishing! As for accessing cells within a file, are you asking for something like this:

    someCell = myfile.cells[1]
    % do something with the cell here.
    

    Out of curiosity, how would you use such a feature?

    -Ken

  20. Fiona replied on :

    I try to debug/run my .m files or using F5 but it does not work (nothing shows in the command window not even errors) only the cell mode is working but this bug me. Can you please tell me how to be able to use F5 instead of cell mode.

    thank you!

  21. Mike replied on :

    Fiona,

    I am not sure what you mean by F5 does not work. Can you verify:

    (1) That F5 is the keystroke listed next to Run xxx.m in the Debug menu? (where xxx is the name of the file you are editing).

    (2) Does it fail just for one file or all files?

    (3) Try creating a new script with just the following line:
    disp(’hello world’). Press F5.

    (4) What is your computer setup? Are you using windows or a different OS? Are you at the machine running MATLAB or are you connecting over a remote connection?

  22. Tim Smith replied on :

    Another voice for breakpoints here — this looks like it should be fantastically useful, but no breakpoint support makes this feature much less interesting as a rapid code iteration tool, particularly in the context of debugging algorithms. Thanks for a great intro post, though!

  23. Ken replied on :

    Thanks for the feedback Tim — we’ll certainly take it under consideration. Glad you found the post useful!

    -Ken

  24. AV_R replied on :

    I’m also highly interested in having breakpoints in cell mode. I use the cell mode to prototype quickly some code which typically ends up as part of some function. Hence, the ability to debug this code would be very helpful.

  25. Patrik Brynolfsson replied on :

    Another vote for breakpoints in cell mode! I’m really surprised that it’s not implemented from the start, what harm do breakpoints do? For rapid execution of part of the code when evaluating and developing this would be very useful. Knowing which line threw an error is also necessary, why did the guys at matlab not think this was needed in cell mode beats me.

  26. Ken replied on :

    Hi Patrik,

    We didn’t intentionally exclude the ability to use breakpoints in cell mode — this was actually a consequence of the way cell mode was implemented. We’re working to remedy this issue.

    -Ken

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Ken & Mike work on the MATLAB Desktop team.
  • Ori: The current folder shortcut used to be alt-y. Now it is alt-o. However, while in the editor window, alt-o opens...
  • Jose Miguel: Hi First of all, thanks for the post, it is really useful. I am trying to develop a Java GUI within...
  • Ken: Hi Siddharth, There isn’t currently any way to move the docking controls. Feel free to submit an...
  • Phil: I have the same problem as described above with UITable working fine in the Matlab environment but showing no...
  • Siddharth: Is there any way to move the position of the docking controls (or eliminate them through some...
  • Chris: Yes, it is a challenge to organize functions into categories, especially with so many functions. Ken and I...
  • Mike: Thanks for fielding that one, Yair.
  • Yair Altman: Jimmy - if you mean that you wish to include hyperlinks in your function’s help comment, that will...
  • OysterEngineer: Thanks for explaining the Function Browser. I fired it up and gave it a try. It appears that it has...
  • Jimmy: Is there any way to include hyperlinks in a comment, such as the standard help at the beginning of a function?

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