%% Define compressed data filename
compressed_file = 'compressed.mat';

%% Define recovered data filename
recovered_file = 'recovered';

%% Read saved data from compressed file
load(compressed_file);
% Note: Now alphabet, counts and packed_code are restored

%% Compute the length of the message
len = sum(counts);

%% Unpack the arithmetic code
code = bwunpack(packed_code);

%% Arithmetically decode the index n
n = arithdeco(code, counts, len);

%% Recreate the message
msg = alphabet(n);

%% Write copy of data to a file
fid = fopen(recovered_file, 'w');
cnt = fwrite(fid, msg);

%% Check whether write succeeded
if(cnt < len)
  error('Could not write all data');
end

%% Close file
fclose(fid);