% tsaveload  Demonstrates use of save and load functions [PHYS210-DEMO]
%
% save:   Save (write/print) array to a text (ASCII) file.
% load:   Load (read) array from a text (ASCII) file.
% 
% Note: These functions can be used to load and save data in 
% a variety of ways.  Refer to the Matlab help facility for 
% full details.
%-----------------------------------------------------------

%-----------------------------------------------------------
% Generate 2D array (matrix), pwrsout, with 4 columns 
% containing the integers from 1 to 100, their squares, 
% cubes and fourth powers, respectively.
%-----------------------------------------------------------
n = 50;
v = [1:n]';
pwrsout = [v v.^2 v.^3 v.^4];

%-----------------------------------------------------------
% (1) Using save as a function.
% 
% Save the array to 'pwrsout.dat' by invoking save as a 
% function.
%
% When using save within a function or script it is best to 
% use this form.
%
% Arguments: Note that all three must be strings.  Here we 
% use literal strings, i.e. sequences of characters that are 
% enclosed in single quotes, but we could also use Matlab 
% variables that that had previously been assigned string 
% values.
% 
%     'pwrsout.dat'    ->   write the array to a file named 
%                           pwrsout.dat
%     'pwrsout'        ->   write the array pwrsout
%     '-ascii'         ->   write the array in ASCII format
%                           (plaing text)
%-----------------------------------------------------------
save('pwrsout.dat', 'pwrsout', '-ascii')

%-----------------------------------------------------------
% (2) Using save as a command.
% 
% For convenience, we can use save as a command, in which 
% case:
% 
%     (a) The arguments do not have to be strings; i.e.
%         we don't have to surround the filename, etc.
%         with single quotes.
%     (b) The arguments are separated by whitespace, not 
%         commas, and are not enclosed in parentheses ().
%  
% Output is to the file pwrsout1.dat
%-----------------------------------------------------------

save pwrsout1.dat pwrsout -ascii;

%-----------------------------------------------------------
% (3) Using load.
%
% Load the data from the file pwrsout.dat into the 2D array 
% (matrix) pwrsin.  
%
% Here we MUST use the regular "function" syntax and load r
% returns the array that was read, so we use load on the 
% right hand side of an assignment to prwsin. 
%
% We do not need to supply '-ascii' to load (although it
% won't hurt if we do): Matlab will automatically determine 
% that the file is plain-text.
%-----------------------------------------------------------
pwrsin = load('pwrsout.dat');

%-----------------------------------------------------------
% Determine the size of the array that was read, and plot 
% the third column vs the first.
%-----------------------------------------------------------
[nr_pwrsin, nc_pwrsin] = size(pwrsin)

figure(1);
clf;
plot(pwrsin(:,1), pwrsin(:,2));
title('Plot of data read from pwrsout.dat');
xlabel('i');
ylabel('i^3');

%-----------------------------------------------------------
% Load the file pwrsout1.dat into the array pwrsin1
% make a plot of the fourth column vs the second.
%-----------------------------------------------------------
pwrsin1 = load('pwrsout1.dat');

figure(2);
clf;
plot(pwrsin1(:,2), pwrsin1(:,4));
title('Plot of data read from pwrsout1.dat');
xlabel('i^2');
ylabel('i^4');
