Matlab Utilities

Usage Information

All of the Matlab tools described here, along with their supporting JAR files, are distributed with both Figure Composer and DataNav Builder. Note that you must have Matlab 7.x or later to use these functions. For full details on how to use each function, make sure the requisite M-file is in your Matlab command path, then type help function-name at the command prompt. We strongly recommend that you read the help information carefully.

The supporting JAR files must be in Matlab's Java class path or these functions will not work; instead, you will see some mysterious error messages in the command console when you try to execute a function. To add the JAR files to the dynamic class path, use the Matlab command javaclasspath({'jarpath1', 'jarpath2', ...}), where 'jarpath1', 'jarpath2', ... are full file system pathnames identifying the required JAR files. Of course, you have to remember to execute this command each time you start Matlab. A more convenient approach is to use the javaaddpath command in your startup.m file: javaaddpath jarpath1, javaaddpath jarpath2, ...

Convert a Matlab figure to a DataNav figure: matfig2fyp()

Perhaps the most difficult obstacle to composing scientific figures in DataNav is getting the actual raw data into the application in a manner that is convenient for users. Our first attempt to address this roadblock was the utility function putdatanavsrc(), which lets the Matlab user populate a file with any number of DataNav-formatted data sets. Then, in DataNav itself, the user can design the figure from scratch and load the necessary data from that file. However, this strategy requires a certain familiarity with the different data set formats supported by DataNav, learning how to use the relatively complex putdatanavsrc() function, and then the additional work of creating a figure from scratch in DataNav and loading data into it.

Since so many researchers in neuroscience (and other areas of study) are accustomed to analyzing their data and preparing figures in Matlab, it would be extremely helpful if a Matlab figure could be translated directly -- data and all -- into a DataNav FypML figure. That is exactly what matfig2fyp(H, F) does. It takes the handle H of an open Matlab figure, converts it to an equivalent DataNav figure, and saves it to the file F. This file can then be opened in Figure Composer, and the figure modified and embellished as needed to prepare it for scientific publication. 

Of course, many Matlab figures cannot be converted because DataNav does not support all the features and specialized plotting functions that Matlab offers. Nevertheless, in its current state, the function can convert a Matlab surface or image object to a DataNav contour node (in the "heatMap" display mode); it can generate trace nodes rendering ptset, series, mset, or mseries data from Matlab line series plots; it can convert specialized 2D plots generated by Matlab's polar(), rose(), compass(), bar(), area(), pie(), scatter(), contour(), and contourf() functions; and it can convert raster train plots generated by the dn_rasterplot() function, described below. As of Version 5.0.0, it can translate most 3D plots generated by Matlab's surf(), scatter3(), and stem3() functions. It translates each Matlab axes in the figure into a DataNav graph node -- or graph3d node for 3D plots --, preserving the axis ranges, labels, and major tick marks if possible, and it will replicate any legends and color bars present in the figure. We will continue to enhance its capabilities as users provide feedback.

Save a Matlab FIG file to be imported by DataNav: savefigasis()

As of Version 4.2.3, Figure Composer and DataNav Builder can import a Matlab figure saved as a MAT file with the .fig extension. The conversion code is the same as that used by the matfig2fyp() utility. However, there are two issues with the direct import feature:

The savefigasis() utility is provided to help you skirt these two issues. It's essentially a wrapper for the Matlab built-in function hgsave(). It calls that function with the "-v7" argument to ensure the FIG file is saved in the storage format (MAT Level 5 with compression) that Figure Composer can handle. In addition, it sets select properties on all "axes" and "scribe.colorbar" objects to ensure that the exact axis ranges and tick mark sets are explicitly stored in the FIG file. We recommend using it to save any Matlab FIG file you plan to import directly into FC.

Inject a data set, text, or a graph into a FypML figure: put2fyp(), putdata2fyp()

Suppose you have a complete, nicely formatted figure in Figure Composer. It's ready for publication, but then you get comments back from reviewers that prompt you to revise your analysis somewhat. Now you need to replace several of the data sets in your figure, and you'd like to do that without having to rebuild the figure from scratch. This is where the Matlab utility function put2fyp(F, 'data', ID, X, Y, ...) may come in handy. It lets you inject a data set prepared in Matlab directly into an existing FypML figure without changing how that data is rendered. You can either overwrite the existing FypML file (the F argument) or save the updated figure to a different file. 

The utility replaces the little-used and now-deprecated putdatatofig() function. Unlike that function, put2fyp() shields the user from the nitty-gritty details of the various FypML-supported data formats. The data to be injected is specified only by an x-coordinate vector and a y-coordinate vector or matrix, a natural way for Matlab users to present or prepare 2D data sets. The function examines the X,Y arguments and the data set being replaced (identified by the ID argument) in order to infer what the format and parameters of the new data set should be. Best of all, you can optionally preview the revised figure before saving it, which can save you time if you make a mistake in the script that generates the data. You no longer have to save the figure, open it in Figure Composer, discover your mistake, and then go back to Matlab to fix it! To make it simpler to use, put2fyp() only lets you inject one data set at a time into a figure. This should not be a significant limitation -- just invoke the function once for each data set you need to replace.

To use put2fyp() effectively, you need to know the character string identifier (the ID argument) of each raw data set you want to replace in your figure. This remains a barrier to the function's adoption for many users, who tend not to assign "memorable" set IDs to the data sets in their figures. Also, to specify less-intuitive data sets like raster-type data or data points with x- and/or y-standard deviations, you'll still have to dive into the detailed information provided in the Matlab command window by help put2fyp.

The put2fyp() utility is actually an enhanced version of the older putdata2fyp(). In addition to injecting raw data, the utility also lets you make other changes to an existing figure:

These commands became possible with FypML schema changes in versions 4.6.2 and 4.7.0, particularly the introduction of the id attribute for graph, label, textbox nodes. This attribute is similar to the data set ID mentioned above, except that it is optional (typically, you'll "ID" only those graphic objects that you might want to change via script) and can contain any Unicode character supported in FC.

As of version 4.7.3, two additional operations give you some control over layout of the graphs within a FypML figure:

When using the 'addgraph' operation to layout 2D graphs in a multi-plot figure, keep in mind that the argument L defines the bounding box for the graph's data window, and the graph's axes are typically drawn outside that window. The amount of space occupied by an axis will depend on a number of graph properties (axis label offset, axis offset from data window, tick mark size, font size). In the 3D case, the graph will be roughly centered around (x,y), but its actual size depends on its backdrop style, the values of (w,h,d), and the graph's rotation and elevation angles. It is reasonable to expect that you will have to "tweak" the locations and sizes of graphs within a multi-plot figure created via put2fyp().

[NOTE: While put2fyp() is intended to replace putdata2fyp(), the latter function is still distributed with FC so that existing user scripts need not be changed. However, putdata2fyp() is considered "obsolete" and will be removed in a future release.]

Create a "raster train" plot in Matlab: dn_rasterplot()

A common figure in neuroscience circles is the "raster plot", which presents spike occurrence times as a series of short vertical lines along a common baseline parallel to the X-axis. Typically, a number of these "raster trains" are presented in the same graph, spaced vertically as in the example below:

Compatibility with Matlab R2014b

Matlab release 2014b (R2014b) introduced a major overhaul in its figure graphics, including significant changes in its Handle Graphics (HG) object infrastructure. Both the matfig2fyp() utility and Figure Composer's FIG file import engine must analyze the HG tree structure in order to prepare a Java version of that tree that contains enough information to translate the original Matlab figure into a FypML figure. Some of the graphics changes in R2014b created compatibility issues with FC's import code.

Version 4.6.2 addressed these issues, so you should now be able to run matfig2fyp() in R2014b and import FIG files generated by that release. (Unsupported features and plots will still fail to import, of course). This version also adds a new color map that is somewhat like Matlab's new default color map, so figures relying on that color map should look similar when converted by matfig2fyp() or imported directly into FC.

Feel free to upgrade your Matlab installation to R2014b. If you run into any problems using matfig2fyp() in that release, or you are unable to import a FIG file generated by R2014b, send us the FIG file along with an explanation of what went wrong. Thanks!

Since Matlab does not offer a plotting function for this kind of presentation of 1D discrete-time event data, we developed a utility function that does: dn_rasterplot(R, ...).  It prepares a specialized polyline that draws a single vertical hash mark for each event in R, using (NaN, NaN) for every third point in the polyline so that the hash marks are not connected to each other. The argument R can take two forms: 

For a complete description of the function and its other arguments, download dn_rasterplot.m, install it in your Matlab command path, and type help dn_rasterplot on the command line.

NOTE that a Matlab figure containing raster trains generated with dn_rasterplot() can be translated into a similar DataNav figure using matfig2fyp(). Each raster train will be presented in a raster node, and the actual event data will be drawn from the polyline's X- and Y-vectors and stored in the DataNav figure as a data set in raster1d format. In fact, the graph above is an image of a DataNav figure prepared by using dn_rasterplot() to create the Matlab figure, then matfig2fyp() to convert it to DataNav FypML format.

Read/write DataNav data set source files in Matlab: getdatanavsrc(), putdatanavsrc()

While the Dataset Editor Dialog is reasonably easy to use and flexibly supports cut-and-paste from applications like Excel, it remains a relatively laborious and time-consuming method for pushing data into a DataNav figure. It is really intended more as an avenue for viewing and editing the raw data, or for creating very small data sets from scratch. Furthermore, it would be impractical as a means of entering data into a DataNav portal, since a portal hub will typically contains hundreds, if not thousands, of data sets. What is needed is an automated means of storing one or more data sets in a source file that can then be read by the DataNav applications. 

Data set source files in Phyplot -- the predecessor to DataNav's Figure Composer -- were plain-text files containing columns of floating-point numbers with little or no "metadata" describing the data (ID, format type, parameters like the sample interval dx). A blank line in the file separated one data set from the next. The advantage of such a simple format was that it was easy for any user -- regardless what tools they used to generate their data -- to repackage it in a form that Phyplot could handle. 

However, this simple format has a number of serious disadvantages that make it ill-suited for use in the DataNav apps: 

DataNav addresses these problems by supporting a binary data source file format with a table of contents at the beginning of the file that includes summary information and file offsets for each data set stored in the file. In addition, it supports an annotated plain-text format with a table of contents. This format is less compact than the binary format, but it is better suited for transmission over the Internet. The recommended (but not required, since each format includes a header for identification purposes) file extension for the binary format is .dnr (formerly, .dnb); for the annotated format, .dna or .txt

Of course, introducing an opaque "proprietary" format means we need to provide tools for reading and writing these files. Since members of the Lisberger laboratory use Matlab extensively to analyze their data, we developed the complementary Matlab M-functions, putdatanavsrc() and getdatanavsrc(). Use putdatanavsrc() to write Matlab-generated data in DataNav-compatible data source files in either format -- although you are encouraged to use the binary format exclusively unless you have a really good reason not to do so! With it you can store any number of data sets in a single file, as long as each data set has a unique identifier. Use getdatanavsrc() to read datasets from a DataNav data source file back into Matlab. The source file may have been generated by a DataNav application, by the Lisberger lab's analysis program JMWork, or by a previous call to putdatanavsrc()