


PURPOSE: dumps a number of variables to an ASCII file. Variables are
dumped using single columns per variable and should be equal
length.
-------------------------------------------------------------------
USAGE: dump_ascii_vars(data,fname,varnames,separator,nodata)
where: [data] is a 1-dim cell-array containing the 1-dim variables to be dumped
[fname] is the output file name (string)
[varnames] (optional) cell array to specify variable names to be dumped as
first line in file. Must have same number of elements
as cell array {data} has variables.
[separator] (optional) CHAR used to separate column values,
default is ' ' (whitespace).
[nodata] (optional) is the value NaNs should be converted to
before dumping. Default is 'NaN'.
-------------------------------------------------------------------------
OUTPUTS: ASCII File [fname]
NOTE: Variables should be in column form of 1 x n. No of records dumped is that
of the shortest variable, if not equal length. Designed to dump
variables to ASCII format that can be imported to SPSS.
-------------------------------------------------------------------------
See also:
Functions used: fopen, fprintf, ischar, exist
Felix Hebeler, Geography Dept., University Zurich, Juli 2006.

0001 function [] = dump_ascii_vars(data,fname,varnames,separator,nodata) 0002 % PURPOSE: dumps a number of variables to an ASCII file. Variables are 0003 % dumped using single columns per variable and should be equal 0004 % length. 0005 % ------------------------------------------------------------------- 0006 % USAGE: dump_ascii_vars(data,fname,varnames,separator,nodata) 0007 % where: [data] is a 1-dim cell-array containing the 1-dim variables to be dumped 0008 % [fname] is the output file name (string) 0009 % [varnames] (optional) cell array to specify variable names to be dumped as 0010 % first line in file. Must have same number of elements 0011 % as cell array {data} has variables. 0012 % [separator] (optional) CHAR used to separate column values, 0013 % default is ' ' (whitespace). 0014 % [nodata] (optional) is the value NaNs should be converted to 0015 % before dumping. Default is 'NaN'. 0016 % ------------------------------------------------------------------------- 0017 % OUTPUTS: ASCII File [fname] 0018 % 0019 % NOTE: Variables should be in column form of 1 x n. No of records dumped is that 0020 % of the shortest variable, if not equal length. Designed to dump 0021 % variables to ASCII format that can be imported to SPSS. 0022 % ------------------------------------------------------------------------- 0023 % See also: 0024 % Functions used: fopen, fprintf, ischar, exist 0025 % 0026 % Felix Hebeler, Geography Dept., University Zurich, Juli 2006. 0027 0028 %% check number and type of arguments 0029 if nargin < 2 0030 error('Function requires at least two input arguments'); 0031 elseif ~ischar(fname) 0032 error('Input argument must be a string representing a filename'); 0033 end 0034 0035 if ~exist('separator','var') 0036 separator=' '; 0037 end 0038 0039 if ~exist('nodata','var') 0040 nodata='nan'; % not really necessary 0041 else 0042 for i=1:length(data) 0043 data{i}(isnan(data{i})) = nodata; 0044 end 0045 end 0046 0047 fid = fopen(fname,'wt'); 0048 cols=length(data); 0049 a=nan(1,cols); 0050 for i=1:cols 0051 a(i)=length(data{i}); 0052 end 0053 rows=min(a); 0054 0055 if exist('varnames','var') 0056 for i=1:cols-1 0057 fprintf(fid,'% s',varnames{i}); 0058 fprintf(fid,'% s',separator); 0059 end 0060 fprintf(fid,'% s',varnames{i+1}); 0061 fprintf(fid,'%s\n',' '); 0062 end 0063 % write data, line by line 0064 for r = 1:rows 0065 % write data, column by column for each var 0066 for i = 1:cols-1 0067 fprintf(fid,'% f',data{i}(r)); 0068 fprintf(fid,'%s',separator); 0069 end 0070 fprintf(fid,'% f',data{i+1}(r)); 0071 fprintf(fid,'%s\n',' '); 0072 end 0073 % close file 0074 fclose(fid);