Posts Tagged MATLAB

SciLab and GreenSciLab

SciLab and GreenSciLab – open source tools for systems modeling

Introduction to SciLab
SciLab is an open source software package developed at INRIA (France). It was introduced as an open source alternative to MATLAB. It is a vector based program. It has constantly undergone vital changes ever since its inception in 1994. It also features a wide variety of tools for various engineering and mathematical applications. Its main features include:

  • Hundreds of mathematical functions
  • High level programming language
  • 2-D and 3-D graphics
  • Advanced data structures and user defined data types
  • Xcos: hybrid dynamic systems modeler and simulator

However, SCILAB does not include a user friendly interactive environment as MATLAB does. The latest release version crashes frequently when I tested its functions. Except the drawback, SCILAB has a lot of useful open source packages. MATLAB m-file can be run in SCILAB after a minimum conversion. So, SCILAB as a free open source alternative to MATLAB is not too bad.

Figure 1

GreenLab and GreenSciLab
GreenSciLab is a stochastic, functional and interactive models for plant growth and architecture. GreenLab was launched in 1998 as LIAMA project, an Associate Team of INRIA Digiplant Project. GreenScilab is a free software developped in LIAMA and INRIA. It’s a powerful tool for cultivating virtual plants in Scilab Environment. GreenScilab is a combination of GreenLab functional structural plant model and Scilab open source software for scientific computation.

Figure 2


Based on my experience with GreenLab and GreenSciLab, it is not mature enough for public use, still restricted to the model developer and small group of people who have close connection with INRA and related project. It will be very interesting to know how these tools are improved and advanced. They will be good tools to study canopy architecture and ideal plant type for crop improvement. Modern plant breeding heavily relies on field observations and data interpretation. If these tools get mature enough and can be applied to plant breeding process, that will be a dramatic step and contribution for scientific society.


  1. Greenlab at INRIA –
  2. LIAMA at CAS –
  3. GreenSciLab at CAS –
  4. SciLAB home –

Tags: , , , , ,

Matlab Toolbox: Fuzzy Clustering and Data Analysis Toolbox

I googled “fuzzy clustering and matlab” and wanted to find some tools for data clustering today. I came across a very good MATLAB toolbox at In the web page, you can find source code and a detailed documentation that describes several popular clustering methods and validation methods, too. The Toolbox is a collection of MATLAB functions. The toolbox provides four categories of functions: 1) Clustering algorithms, 2) Evaluation with cluster prototypes, 3) Validation and 4) Visualization (See the following figure). In addition, it also provides some real examples that you can use them as start point for your project. To learn more you can visit author’s website or download source code and documentation here.


MATLAB Source code –
Toolbox documentation – FuzzyClusteringToolbox


Tags: , , ,

Graph Digitization in MATLAB

Sometimes we need digitize graphs from scientific papers because we can not get the original data. The digitized data can be used to fit model or redraw graphs. How can we achieve the goal? At the beginning I think we can do that by reading the image data and classifying the data by a given threshhold. Then put the data to spreadsheet (EXCEL) and do further precessing. After I tried, I found it was difficult to do it in EXCEL.

I turned to MATLAB. I found there are several general functions in MATLAB that can handle this problem very easily. By using imread function to read the image data to memory, and image function to show the image, then ginput to get data points in the graphs. After all data point is digitized, use linear regression method to transfer the x and y coordiate values to actual values with the units used in the graph. Then show the data again and save them to text files.

The following code is a complete m function. You can modify it to fit your problem. The arguments of the function are:

  1. imgfilename : the filename of image file, must in the same directory as the m file is;
  2. xp : the actual values of X axis, a row vector;
  3. yp : the actual values of Y axis, a row vector;
  4. line1 : the name of the first line, a string;
  5. line2 : the name of the second line, a string;

The results will be saved to two seperate text files, one file per line. The first column is X value, the second column is Y values. In my case, X is time and Y is gene expression level.

function digitize(imgfilename,xp,yp,line1,line2)
a = imread([imgfilename '.bmp']);
A1=[ones(size(TIME)) TIME];
A2=[ones(size(TOC1)) TOC1];
XLerTime=[ones(size(XLer)) XLer]*C1;
YLerToc1=[ones(size(YLer)) YLer]*C2;
XlhyTime=[ones(size(Xlhy)) Xlhy]*C1;
YlhyToc1=[ones(size(Ylhy)) Ylhy]*C2;
hold on;
DATA1=[XLerTime YLerToc1];
DATA2=[XlhyTime YlhyToc1];
save([imgfilename line1 '.txt'],'DATA1','-ascii');
save([imgfilename line2 '.txt'],'DATA2','-ascii');

After the simple version was finished. I told my major professor and he suggest me to do some statistical tests for the digitizing processing. I rewrote the program. The source code is listed below. In this version, argument ‘lines’ has changed to a cell array, it can accommodate as many lines as possible according to your specific graphs.

function digitize(imgfilename,xp,yp,lines)
% This function can digitize as many lines as you indicate in 'lines'
% imgfilename - the filename of image file, a string
% xp - the actual data value of data points in X axis, a row vector
% yp - the actual data value of data points in Y axis, a row vector
% lines - the string name of lines, a cell array
a = imread([imgfilename '.bmp']);
% number of data points in X axis
% number of data points in Y axis
% number of lines
nline = length(lines);
% number of times to digitize axes
naxes = 3;

Y = []; % all Ys put into one vector
YY = []; % all Ys put into a matrix
% get the data points in Y axis for several times
for i=1:naxes
    Y = [Y;y];
    YY = [YY y];
% pool the data points from three repetition together
A=[ones(size(Y)) Y];
% linear regression model for Y axis
yps = repmat(yp,1,naxes);
% calculate the earror value
ym = mean(YY,2); % mean for each data point
for i=1:size(YY,2), YY(:,i)=YY(:,i)-ym;, end
yy = reshape(YY,1,size(YY,1)*size(YY,2));
yse = std(yy)
yse = CY(2) * yse

X = []; % all Ys put into one vector
XX = []; % all Ys put into a matrix
% get the data points in X axis for several times
for i=1:naxes
    X = [X;x];
    XX = [XX x];
% pool the data points from three repetition together
A=[ones(size(X)) X];
% linear regression model for Y axis
xps = repmat(xp,1,naxes);
% calculate the earror value
xm = mean(XX,2); % mean for each data point
for i=1:size(XX,2), XX(:,i)=XX(:,i)-xm;, end
xx = reshape(XX,1,size(XX,1)*size(XX,2));
xse = std(xx)
xse = CX(2) * xse

% get the data points for all lines
% after digitization of one line, press return key to advance to next line
XLine = cells(nline,1);
YLine = cells(nline,1);
for i = 1:nline
    XLine{i}=[ones(size(xLine)) xLine]*CX;
    YLine{i}=[ones(size(yLine)) YLine]*CY;
for i = 1:nline
    hold on;
    DATA=[XLine{i} YLine{i}];
    save([imgfilename lines{i} '.txt'],'DATA','-ascii');

This version really aspired me. It is not easy to use it because if you input some thing wrong. You need stop the program and run it again until you did everything right. MATLAB provide a tool, GUIDE, to make graphic user interface. Just by trying, I started to make a UI program to digitize the graph. And I succeeded. See the following screenshot.

In the GUI version, users can do the digitization one by one. The order is not very important. You can digitize Y axis, ten X axis. After that test the perpendicular property of two axes. Then digitize lines one by one as long as you give a different name for a line. The program can save the digitized data to different files. You do not need to do it no break. You can do it pierce by pierce as long as the window is not closed. This version give user the maximum flexibility to finish the work.
Because the source code of graphic user interface is too long. I just put it into the zip file. Please download the program and run it under MATLAB 6.5. Download all source code here – digitizeit



Tags: , , , , , , ,

Build a boolean network in MATLAB

To build a boolean network is pretty simple in MATLAB. It does not need ODE function to solve the problem. Just put the rate of change logic equations in a ordinary equation, to calculate several steps (step size always 1). You can get the answer. The sample code is provided here.

function test;
a = 1;
b = 1;
c = 1;
for i = 1:10
    [a, b, c] = calabc(a, b, c);
    [a b c]

function [a,b,c] = calabc(a0, b0, c0)
a = b0;
b = a0 | c0;
c = (a0 & b0) | (b0 & c0) | (a0 & c0);

Tags: ,

Concept of using multiple PCs within LAN for parallel computing and its implementation in MATLAB



  1. 减少函数的调用,不仅仅指自己写的函数,对内建函数的调用也要尽可能地减少;
  2. 不用或少用全局变量以及结构变量;
  3. 预先申请数据空间,减少动态改变数组大小的语句;
  4. 将所有调用函数集中到一个函数中,使用mcc命令将其编译成mex格式的可执行文件;
  5. 尽可能将循环化为向量,然后进行矩阵运算;




function [dap] = master(x,xdata,nummachines)
if nargin < 3
    nummachines = 6;

for i=2:nummachines
    save(['slave' num2str(i) 'ok.txt'],'i','-ascii');
switch nummachines
    case 1
        slave1data = xdata;
    case 4
        slave1data = xdata(:,1:28);
        slave2data = xdata(:,29:48);
        slave3data = xdata(:,49:60);
        slave4data = xdata(:,61:end);
    case 6
        slave1data = xdata(:,1:20);
        slave2data = xdata(:,21:36);
        slave3data = xdata(:,37:47);
        slave4data = xdata(:,48:55);
        slave5data = xdata(:,56:66);
        slave6data = xdata(:,67:end);

for i=2:nummachines
    save(['slave' num2str(i) 'x.txt'],'x','-ascii');
    save(['slave' num2str(i) 'data.txt'],['slave' num2str(i) 'data'],'-ascii');

for i=2:nummachines
    delete(['slave' num2str(i) 'ok.txt']);

dap = atclocktrainfun(x,slave1data);

for i=2:nummachines
    while ~exist(['slave' num2str(i) 'result.txt'])

for i=2:nummachines
    while exist(['slave' num2str(i) 'resultok.txt'])

for i=2:nummachines
    dap = [dap load(['slave' num2str(i) 'result.txt'])];
    delete(['slave' num2str(i) 'result.txt']);


function slave(i)
STOP = 0;
save(['slave' num2str(i) 'resultok.txt'],'i','-ascii');
while ~STOP
    STOP = exist('stop.txt');
    if STOP, break; end

    while ~exist(['slave' num2str(i) 'x.txt'])
        STOP = exist('stop.txt');
        if STOP, break;end
    while ~exist(['slave' num2str(i) 'data.txt']);
        STOP = exist('stop.txt');
        if STOP, break;end

    while exist(['slave' num2str(i) 'ok.txt']);
        STOP = exist('stop.txt');
        if STOP, break;end
    x = load(['slave' num2str(i) 'x.txt']);
    xdata = load(['slave' num2str(i) 'data.txt']);
    delete(['slave' num2str(i) 'x.txt']);
    delete(['slave' num2str(i) 'data.txt']);

    target = atclocktrainfun(x,xdata);
    save(['slave' num2str(i) 'result.txt'],'target','-ascii');
    delete(['slave' num2str(i) 'resultok.txt']);

Tags: , , , , , , ,