SeeMore: A Struct Viewer for the MATLAB console

· Programming

Everything to String? How the simple MATLAB script “seemore” can cure the curse of the console.

For about 50 years programmers had learned to love their monospaced console output. For log files, debugging output or even regular results hardly anything could seriously challenge the text console as the primary output of choice. However, a new feature in recent versions of Google’s Chrome browser should serve as a wake up call to the ASCII purists. This document highlights the innovation in Chrome and introduces the simple script “seemore” to bring this idea to the MATLAB console.

The Chrome console

Like most programming environments the developer tools Chrome offers an interactive console which evaluates JavaScript statements on-the-fly and prints the result as strings. But, these strings aren’t static. They can provide some level of interaction, which allows users to change the level of result detail after they issued their command. The following example demonstrates this ability. Let’s assume we wanted to see the content of current document being displayed in a browser window. Following the W3C standard the document.documentElement contains the current document. Any request to log this on the screen surely leads to a dilemma. If the entire document is shown, you would be overwhelmed by abundant detail. If you only show information about the root node, essential information will be hidden. The Chrome way of solving this dilemma is not to dump the object into immutable ASCII text. Instead, the dumped object is shown in an interactive application. Users can still change the object’s display on the screen, without changing the presented data. How much more usable would consoles be today if it had not been the spread of Java that popularized the universal presence of the toString() method. Instead, the world should have gone for a toInteractiveDataView() method.

Everything to String? Chrome’s logging output is far more flexible than the Firefox console. The displayed output can dynamically change its level of detail.

The MATLAB console

The console output of MATLAB has a little recorgnized feature that makes it possible to get more out of the console than pure text. MATLAB can interpret certain HTML inspired link tags that perform arbitrary commands when clicked. This feature can be used to display partial output, that expands at the user’s request. The basic layout of a MATLAB function that presents interactively expanding data can be seen here:

X= eye(5);
fprintf('<a href="matlab:X">5x5 matrix, click to expand</a>\n')

We can continue to build our interactive data with consecutive data refinement. Consider the following function f that formats 5 elements of a matrix, followed by a link to show more.

X= eye(5);
f= @(x) [mat2str(x(1:5)), '<a href="matlab:disp(f(', mat2str(x(6:end)), '))">show more</a>'];
disp(f(X))

Type “seemore” to see more

A particularly annoying short coming of MATLAB’s console is lacking support for the display of structs. Internal structures of MATLAB do heavily rely on structs to store complex input data. Yet, the on-screen serialization into strings runs into the dilemma of showing the right level of detail. Thetaris has developed a simple tool called “seemore”. This tool uses the above tricks to adaptively uncover the information in structs without opening distracting new windows. Everything is nicely integrated into your console.

The screenshots below demonstrate the effect of  “seemore” when applied to a nested structure returned by MATLAB’s “lasterror” command. By default MATLAB does not show the nested content of the subfield named “stack”. With “showmore” this shortcomming is resolved. The user can click and drill into the result as needed.

The “seemore” command can either be called without arguments to work on the latest output from your console, or present an adaptive display of any variable in your workspace. Try it out now!

Conclusion

MATLAB’s console has a nice feature to format links, which execute arbitrary commands when clicked. This feature can be used to output data onto the console and allow users to define their level of detail as needed. This can save significant amounts of programming time. You no longer need to retype or adjust commands only to define your output format. Save your time for more creative work.

Unfortunately, the presented feature does not allow us to make the data collapse, after it has expanded. Additionally, the expanded data will always show at the bottom of the command screen, rather than integrating nicely into the structured output view. So there is lots of work left for The MathWorks improving the MATLAB console experience.

The suggested use of the console is obviously not limited to MATLAB. After Chrome has taken the lead, the race is open to modernize the console. With HTML as a standard format this idea could be applied to every log file, every debug output and every result file.

2 Comments

Comments RSS
  1. Hello there! I know this is kinda off topic however I’d figured I’d ask.
    Would you be interested in trading links or maybe guest writing a blog post or vice-versa?
    My website discusses a lot of the same subjects as yours and I feel we could greatly benefit from
    each other. If you’re interested feel free to send me an e-mail. I look forward to hearing from you! Wonderful blog by the way!

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.