Hy3S -- Hybrid Stochastic Simulation for Supercomputers
Home | Examples | Download | Developers | Publications | SF Project Page

Useful Tips

Using the NetCDF Toolbox for Matlab, you can read data from NetCDF files into Matlab variables for analysis and plotting. If you've correctly installed the Toolbox, you can type the command netcdf at the Matlab prompt which lists a very dense help menu that describes how to create, define, write, and read NetCDF dimensions, variables, and attributes. For your convenience, here's a quick primer on how to read the solution data and plot it.

To open the NetCDF file in read-only or write mode, the command is

nc = netcdf('filename') or nc = netcdf('filename','write')

Replace filename with the name of the file containing the model and solution data, which was created by the GUI. The Matlab variable nc will contain the file object. To read a variable from the file, the command is

data = nc{'VariableName'}(:,:,:);

where data can be any Matlab variable name, and VariableName can be replaced with the name of any variable inside of the NetCDF file. Remember to use squiggly brackets around the single quoted variable name. The trailing parentheses and colons tell the Toolbox to retrieve all of the data from the NetCDF variable and place it in the Matlab variable named data.

The state data of the simulation is stored in a NetCDF variable named State and the time data is stored in Time. To read these variables into Matlab, type

X = nc{'State'}(:,:,:);
T = nc{'Time'}(:);

Then you can plot the data using Matlab's plot command:


The State NetCDF variable is three dimensional (Number of Trials x Number of Time Points x Number of Species) while the Time variable is one dimensional (TimePoints x 1). That's why there are 3 colons for reading the State variable and only one for Time. The NetCDF Toolbox also allows you to read only a slice of the data. To read only the 5th species, you can type

X = nc{'State'}(:,:,5);

To read only the 30th through 50th time index, type

X = nc{'State'}(:,30:50,:);

To read only every other trial from the first to 1000th, type

X = nc{'State'}(1:2:1000,:,:);

Any combination of the above selections will also work. The syntax becomes extremely useful when, for example, you wish to calculate the probability distribution of the solution at a certain time point. If the program performed many independent simulations of a system you can read in those trials at a specific time point and for a specific chemical species and plot the distribution using

X = nc{'State'}(:,Time Index,Species Index);
[P, Bins] = hist(X,Number of Bins);
P = P ./ sum(P)

The first line reads the data from the NetCDF file, the second uses Matlab's histogram built-in function, the third normalizes the frequency count of the histogram into a probability distribution, and the fourth plots the distribution over the centers of the bins. By increasing the number of independent simulations of a system, you can create better probability distributions.

To get a list of all of the variables inside a NetCDF file, type

listofvars = var(nc);
varnames = ncnames(listofvars)

You can also define new variables, dimensions, and attributes (meta data) using the Toolbox as well as write data directly to NetCDF files from the Matlab prompt. Refer to the Toolbox's help menu (type 'netcdf' at the Matlab prompt) for details.