% Unit 4 (MATLAB Course @ UAM)
% (c) Alexander Knebe

%=================================================================
% read and write the galaxy catalogue 'GalaxiesInTheUniverse.dat'
%=================================================================

% OPTION 1: using load/save
%---------------------------
clear all

% read in the data
load('Galaxies.dat');
save('file1.dat','Galaxies','-ascii');

%% OPTION 2: using fscanf/fprintf
%--------------------------------
clear all

% read in the data (Note: there are 4 columns)
fid      = fopen('Galaxies.dat','r');
Ncolumns = 4
data     = fscanf(fid,'%f %f %f %f');
Nrows    = fix(length(data)/Ncolumns);
data     = reshape(data,Ncolumns,Nrows)';
fclose(fid);


% write the data again
fid = fopen('file2.dat','w');
fprintf(fid,'%f %f %f %f\n',data');
fclose(fid);