Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


wryte3.m

Go to the documentation of this file.
00001 function [err,Fname] = wryte3 (M,Fname,Opt)
00002 %
00003 %   [err,UsedName] = wryte3 (M,Fname,[Opt])
00004 %
00005 %Purpose:
00006 %   Write matrix M to a text file 
00007 %   
00008 %   
00009 %Input Parameters:
00010 %   M : NxK matrix
00011 %   Fname : string containing the file name to write the matrix to
00012 %   Opt is an optional structure with the following fields
00013 %     .Space string specifying whether to use a space character 's' or 
00014 %             a tab 't' or a comma ',' between numbers on the same line.
00015 %             The default is 's'. There won't be a comma after the last number.
00016 %     .OverWrite specifies whether you want to allow for overwrite
00017 %          Options are 'y' for yes, 'n' for no and 'p' for prompting
00018 %          the user for a new name. 
00019 %          You can also use 'a' for append.
00020 %          Default is 'n'
00021 %     .Fast If it is set to 'y', the function will try to use matlab's
00022 %          save function which is fast, but writes the results in IEEE format.
00023 %          like 0.00034e5. Not pleasing to the eye ... Default is 'n'.
00024 %          Note that there is no need to use the fast option with vectors (Nx1),
00025 %          They are written very efficiently
00026 %     .verbose (0/1), default is 0
00027 %
00028 %Output Parameters:
00029 %   err : 0 No Problem
00030 %       : 1 Mucho Problems
00031 %   
00032 %   UsedName : Filename M was saved to 
00033 %       (it's the same as Fname, unless you're prompted to change it)
00034 %      
00035 %More Info :
00036 %   
00037 %
00038 %     Author : Ziad Saad
00039 %     Date : Thu Apr 23 14:41:43 CDT 1998 
00040 
00041 
00042 %Define the function name for easy referencing
00043 FuncName = 'wryte3';
00044 
00045 %initailize return variables
00046 err = 1;
00047 UsedName = '';
00048 
00049 %Set the defaults
00050 if (nargin < 3),        Opt = [];       end
00051 
00052 if (~isfield(Opt,'Space') | isempty(Opt.Space)),
00053         Opt.Space = 's';
00054 end
00055 
00056 if (~isfield(Opt,'OverWrite') | isempty(Opt.OverWrite)),
00057         Opt.OverWrite = 'n';
00058 end
00059 
00060 if (~isfield(Opt,'Fast') | isempty(Opt.Fast)),
00061         Opt.Fast = 'n';
00062 end
00063 
00064 if (~isfield(Opt,'verbose') | isempty(Opt.verbose)),
00065         Opt.verbose = 0;
00066 end
00067 
00068 
00069 %Check for bad input
00070 if (isempty(M)),        err = ErrEval(FuncName,'Err_Nothing to Write'); return; end
00071 if (eq_str(Opt.Fast,'y') & eq_str(Opt.Space,',')),
00072         ErrEval(FuncName,'Wrn_Can not run in fast mode  with comma separators, will use slow mow.');
00073         Opt.Fast = 'n';
00074 end
00075 
00076 %Do the deed
00077 %Check for filename existence
00078 op = 'w';
00079 if(filexist(Fname)),
00080         switch Opt.OverWrite,
00081                 case 'n',
00082                         err = ErrEval(FuncName,'Err_FileExist');
00083                         return
00084                 case 'y',
00085                         %all cool proceed
00086                 case 'p',
00087                         while (filexist (Fname) == 1),
00088                                 serr = sprintf ('Wrn_File %s exists, enter new filename:',Fname);
00089                                 ErrEval(FuncName,serr);
00090                                 Fname = input ('','s');
00091                         end;
00092                 case 'a',
00093          op = 'a';
00094       otherwise,
00095                         err = ErrEval(FuncName,'Err_Bad Opt.OverWrite value');  return
00096          end
00097 end
00098 
00099 %Open file for write operation
00100 fid = fopen(Fname,op);
00101 if (fid == -1),
00102         err = ErrEval(FuncName,'Err_Could not open file for write operation');  return
00103 end
00104 
00105 %Write the data
00106 switch Opt.Fast,
00107         case 'y',
00108         %fast mode
00109                 if (eq_str(op,'a')), apnd = '-append';
00110       else apnd = ''; end
00111       if (Opt.verbose), fprintf (1,'%s verbose : Writing %s in Fast Mode ...',FuncName,Fname);  end
00112                 if (eq_str(Opt.Space,'t')),
00113                         eval(['save ' Fname ' M -ascii -tabs -append']);
00114                 elseif (eq_str(Opt.Space,'s')),
00115                         eval(['save ' Fname ' M -ascii -append']);
00116                 else
00117                         err = ErrEval(FuncName,'Err_Delimiter not supported in Fast mode');
00118                         return;
00119                 end
00120         case 'n',
00121         %slow mode
00122                 [ii,jj] = size(M);
00123                 if (jj==1),     %I can go fast on this one !
00124                         if (Opt.verbose),       fprintf (1,'%s verbose : Writing %s in Efficient Mode ...',FuncName,Fname);     end
00125                 
00126                         if (eq_str(Opt.Space,'s')),
00127                                 fprintf(fid,'%g \n',M(1:ii-1)); end
00128                         if (eq_str(Opt.Space,'t')),
00129                                 fprintf(fid,'%g\t\n',M(1:ii-1));        end
00130                         if (eq_str(Opt.Space,',')),
00131                                 fprintf(fid,'%g,\n',M(1:ii-1)); end
00132                         %Now put the last value in
00133                         fprintf(fid,'%g\n',M(ii));      
00134                 end
00135                 
00136                 if (jj > 1),
00137                         if (Opt.verbose),       fprintf (1,'%s verbose : Writing %s in Slow Mode ...',FuncName,Fname);  end
00138                 
00139                         jjtemp = jj;
00140                         for (i=1:1:ii),
00141                                 if (i == ii),   jjtemp = jj - 1;        end
00142                                 for (j=1:1:jjtemp),
00143                                         if (eq_str(Opt.Space,'s')),
00144                                                 fprintf(fid,'%g ',M(i,j));      end
00145                                         if (eq_str(Opt.Space,'t')),
00146                                                 fprintf(fid,'%g\t',M(i,j));     end
00147                                         if (eq_str(Opt.Space,',')),
00148                                                 fprintf(fid,'%g,',M(i,j));      end
00149                                 end
00150                                 if (i ~= ii), fprintf(fid,'\n');        end
00151                         end
00152                         %Now put the last number in
00153                         fprintf(fid,'%g\n',M(ii,jj));   
00154                 end
00155         otherwise,
00156                 err = ErrEval(FuncName,'Err_Bad Opt.Fast value');       return
00157 end
00158 
00159 fclose(fid);
00160 
00161 if (Opt.verbose),       fprintf (1,'Done.\n');  end
00162                 
00163 err = 0;
00164 return;
00165 
 

Powered by Plone

This site conforms to the following standards: