function mat2bugs(varargin) %MAT2BUGS - save matlab variables in S-PLUS format for BUGS % %mat2bugs(name1,value1,name2,value2,...) % Writes S-PLUS data specification to the standard output % for given variable names and values. % Any number of name,value pairs can be given. % %mat2bugs(filename,name1,value1,name2,value2,...) % Writes S-PLUS data specification to the given file % for given variable names and values. % Any number of name,value pairs can be given. % %mat2bugs(filename,name1,value1,dim1,name2,value2,...) % Writes S-PLUS data specification to the given file for % given variable names and values. Dim argument can be used % to force use of 1D or 2D tables (value 1 or 2 respectively) % Any number of name,value pairs can be given. % %Examples: % mat2bugs('x',x,'y',y); % mat2bugs('bugs_data.dat','x',x,'y',y); % mat2bugs('bugs_inits.dat','mu',zeros(size(y)),'tau',1); % mat2bugs('bugs_inits.dat','Y',itr{i1},'Y2',itst{i1},1); % %BUGS: (no pun intended) % Supports only scalars and 1D and 2D tables. 3D tables would be % trivial to add. % % (c) jouko.lampinen@hut.fi, 1998-1999 % 2002-11-01 Aki.Vehtari@hut.fi - Added handling for NaN % 2003-01-14 Aki.Vehtari@hut.fi - Option to force use of 1D and 2D tables if ischar(varargin{1}) & ischar(varargin{2}) ofile=fopen(varargin{1},'w'); if ofile<1, error(['Cannot open ' varargin{1}]); end i=2; else ofile=1; i=1; end fprintf(ofile,'list('); while i2, fclose(ofile); end function fprintval(ofile, val) if isnan(val) fprintf(ofile,'NA'); else if ispc %added by Leming Qu 0n 07/22/2003 for the format in WinBUGS 1.4 in Windows tempstr=sprintf('%G',val); if strfind(tempstr,'E+0')>0 tempstr=strrep(tempstr,'E+0','E+'); elseif strfind(tempstr,'E-0')>0 tempstr=strrep(tempstr,'E-0','E-'); end if isempty(strfind(tempstr,'.')) & (strfind(tempstr,'E')>0) tempstr=strrep(tempstr,'E','.0E'); end fprintf(ofile,'%s', tempstr); else fprintf(ofile,'%G',val); end end