function convertToBioBayes(T,D,Names,filename)
%convertToBioBayes Converts Matlab data into BioBayes dataset description
%   convertToBioBayes(T,D,Names,Filename), where 
%    T       is a row vector of time values at which the data has been
%            measured
%    D       is a matrix in which rows correspond to measured variables
%            The number of columns in vector T and matrix D must be equal.
%    Names   is a set of cells which define the names of the variables.
%            The number of elements in this set must be equal to the number
%            of rows in D.
%    filename is a string name of the file to which BioBayes description
%            of the dataset will be written.
%
%    EXAMPLE:
%       T = [1,2,3,4];
%       D = [1 2 3 4; 
%            5 6 7 8];
%       Names = {'m1','m2'};
%       convertToBioBayes(T,D,Names,'test.data');
%
%  See also LOAD.

%   Copyright 2007-2008 University of Glasgow
if (size(T,2) ~= size(D,2))
    fprintf('The size of the time course and the data set do not agree');
end
fout = fopen(filename,'w');
fprintf(fout,'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n<dataset noise="normal" name="Exported from MATLAB" xmlns="http://bisb.gla.org/dataset">\n    <data cols="%d">\n',size(D,1)+1);
    for row=1:size(T,2)
        fprintf(fout,'<row>\n');
        fprintf(fout,'<element value="%f" index="0"/>\n',T(row));
        for col=1:size(D,1)
            fprintf(fout,'<element value="%f" index="%d"/>\n',D(col,row),col);    
        end
        fprintf(fout,'</row>');
    end
    fprintf(fout,'</data>');
    fprintf(fout,'   <condition/>\n    <interpretation>\n        <time col="0"/>\n');
    for col=1:size(D,1)
        fprintf(fout,'<readout expression="%s" col="%d"/>\n',Names{col},col);
    end
    fprintf(fout,'   </interpretation>\n</dataset>\n');
    fclose(fout);
end