function writeout(X,FileName) % WRITEOUT - Writes the contents of a stordat structure to a file % % WRITEOUT writes the contents of structure created with READOUT_ and % writes it to a tab-delimited text file. % % Use as: writeout(X, FileName) % or writeout(X) % % Inputs: X = structure with at least the fields: TIME_TAG and Filename. % WRITEOUT requires that the first Field be Filename. % FileName = The name of the file to write to. If not specified % a dialog box will prompt the user. % % Brian Schlining % 14 Jan 1998 fprintf(1,'\n(WRITEOUT)') %========================== % Get FileName to write to %========================== [r c] = size(X.Filename); if r == 1 [trash XFile XExt] = fileparts(deblank(X.Filename)); % For use with the input dialog box else XFile = 'temp'; XExt = 'raw'; end %===================================================== % Prevent Writing to the original (*.raw) source file %===================================================== NotOK = 1; if nargin < 2 % This while loop prevents writing to the original source (*.raw) file while NotOK [infile inpath] = uiputfile([XFile '.*'],'Filename to write to:',0,0); if infile == 0 return end FileName = [inpath infile]; %if ~strcmp(infile,X.Filename) % this line prevents writing to the same file name if ~strcmp(infile,[XFile '.raw']) NotOK = 0; else warndlg('Output filename must be different from the original filename',... ['Unable to write to ' infile]); end end end fprintf(1,': %s',FileName) % Get filename from the path and store it [trash, File, Ext] = fileparts(FileName); %if strcmp([File Ext],X.Filename) % Prevents writing to a file with the same name if strcmp([File Ext],[XFile '.raw']) error(['Unable to write to ' File Ext ... '. Output filename must be different from the original filename']) return end %================================= % Write the structure to the file %================================= fid = fopen(FileName,'wt'); % Open the File Fields = fieldnames(X); % Get the names of all the fields in the structure ifn = strmatch('Filename',Fields); % find the filename field NumOfFields = length(Fields); % Count the number of fields [Year, Month, Day, Hour, Minute, Second] = datevec(X.TIME_TAG); % Change the data to normal units Second = round(Second); % We don't care about fractional seconds % Deal with Y2k issues %i = find(Year >= 2000); %if ~isempty(i) % Year(i) = Year(i) - 2000; %end % More Y2K issues %i = find(Year < 2000); %if ~isempty(i) % Year(i) = Year(i) - 1900; %end % Change the structure field names to Column headers in the ascii output file for i = 1:NumOfFields if i ~= ifn % ignore the Field of the filename F = Fields{i}; j = findstr(F,'_'); if ~isempty(j) F(j) = '('; F(end+1) = ')'; end if i ~= NumOfFields fprintf(fid,'%s\t',F); else fprintf(fid,'%s\n',F); end end end % Set the bad data flag to -999 for j = 1:NumOfFields if j ~= ifn dat = getfield(X,Fields{j}); i = find(isnan(dat) | isinf(dat)); dat(i) = -999; X = setfield(X,Fields{j},dat); end end % The actual writing of the data happens in this big loop for i = 1:length(X.TIME_TAG) for j = 1:NumOfFields if j ~= ifn if j ~= NumOfFields if findstr(Fields{j},'SN') fprintf(fid,'%i\t',round(getfield(X,Fields{j},{i}))); elseif findstr(Fields{j},'MS') fprintf(fid,'%-i\t',round(getfield(X,Fields{j},{i}))); elseif strcmp(Fields{j},'TIME_TAG') fprintf(fid,'%04i-%02i-%02i %02i.%02i.%02i\t', Year(i), ... Month(i), Day(i), Hour(i), Minute(i), Second(i)); else fprintf(fid,'%8.6f\t',getfield(X,Fields{j},{i})); end else if findstr(Fields{j},'SN') | findstr(Fields{j},'MS') fprintf(fid,'%i\n',round(getfield(X,Fields{j},{i}))); elseif findstr(Fields{j},'MS') fprintf(fid,'%-i\n',round(getfield(X,Fields{j},{i}))); elseif strcmp(Fields{j},'TIME_TAG') fprintf(fid,'%02i-%02i-%02i %02i.%02i.%02i\n', ... Year(i), Month(i), Day(i), Hour(i), Minute(i), Second(i)); else fprintf(fid,'%8.6f\n',getfield(X,Fields{j},{i})); end end end end if ~rem(i,200) fprintf(1,'.') end end fprintf(1,'\n') fclose(fid);