function savedata (fn, x, format)
%SAVEDATA  Save data in text format.
%
%   SAVEDATA('filename', dat);

s = size(x);
assert(length(s) <= 2); % only deal with 2-D data

if nargin < 3, format = '%g'; end

fid = fopen(fn, 'w');
if fid < 0
    error('File create error.');
end

for i = 1:size(x, 1)
    for j = 1:size(x, 2)
        if j > 1, fprintf(fid, ' '); end
        fprintf(fid, format, x(i,j));
    end
    fprintf(fid, '\n');
end
