Info

This question is closed. Reopen it to edit or answer.

Need to see Values/Names not Size and Class/Type

1 view (last 30 days)
James
James on 2 Mar 2013
Closed: MATLAB Answer Bot on 20 Aug 2021
Hi, my code below creates a huffman tree and codewords. Input is a .txt file that is a two column list of ascii numeric values and corresponding probabilities. I have commented out the last 10 or so lines of code that display the output. I would like to be able to just be able to type in the command window, one of my array variable names, such as codes, probs or set_contents, and have an array of VALUES displayed. When I attempt to do that after running the code in its current state, it gives me something like this:
set_contents =
[1x26 double]
Displaying the Size and the Class (or Variable Type, why is it called Class in MatLab?). This information about my variable is useless to me. I need to have the variable's actual value in the arrays, I believe MatLab calls this Name (why is it called Name?). I want to call set_contents and have it display
set_contents =
[.0007]
or whatever the value is. Please help, thanks!
function HuffmanCoding(filename)
% read in table of token and probability values
data = importdata(filename);
% check file, I couldn't figure out how to read the AlphabetFrequency.txt
% file, too many problems with the alphabet values.
ptSize = size(data);
if ptSize(2) ~= 2
error('error: your .txt file must be 2-column table of numeric values.');
end
% arrange and label the information of each column
probTable = [data(1:1:end/2)', data(end/2+1:1:end)'];
sorted_probTable = sortrows(probTable, 1);
asciiVals = sorted_probTable(1:1:end/2)';
probs = sorted_probTable(end/2+1:1:end)';
% set up useful variables
numProbs = length(probs);
for index = 1:numProbs
codes{index} = []; % empty uffman codeword cell vector
set_contents{index} = index; % set containing only this codeword
set_probs(index) = probs(index); % set to store the probability associated with this set
end
% TO SEE WHAT THIS DID, UNCOMMENT THIS CODE
% disp('The tree of sets of tokens(A thru Z #s) and their probabilities:')
% for set_index = 1:length(set_probs)
% disp([ num2str(set_contents{set_index}),' ', num2str(set_probs(set_index))]);
% end
% Create the huffman code tree with a while loop:
while length(set_contents) > 1
% First: Sort the table in ascending order based on probabilities column
% The variable temp_set_probs is only usefull as a "trashcan", since the
% probabilities column needs to stay the same in the long run
[temp_set_probs, sorted_indices] = sort(set_probs);
% Second: Take the 2 smallest probabilities, and assign code values
% For lowest probability add 0:
zeros_set = set_contents{sorted_indices(1)}; % define zeros set
lowest_prob = set_probs(sorted_indices(1)); % corresponding prob
for codes_index = 1:length(zeros_set)
codes{zeros_set(codes_index)} = [codes{zeros_set(codes_index)},0];
end
% For 2nd lowest probability add 1:
ones_set = set_contents{sorted_indices(2)}; % define ones set
second_lowest_prob = set_probs(sorted_indices(2)); % corresponding prob
for codes_index = 1:length(ones_set)
codes{ones_set(codes_index)} = [codes{ones_set(codes_index)},1];
end
% TO SEE WHAT THIS DOES UNCOMMENT THIS CODE
% disp('The symbols, their probabilities and the allocated bits are:');
% for index = 1:length(codes)
% % ...display its bits
% disp([num2str(index),' ',num2str(probs(index)),' ',num2str(codes{index})]);
% end
% Remove the two sets having the lowest probs, append to new set
set_contents(sorted_indices(1:2)) = [];
set_contents{length(set_contents)+1} = [zeros_set, ones_set];
% Remove the two lowest probabilities, sum and append to new set
set_probs(sorted_indices(1:2)) = [];
set_probs(length(set_probs)+1) = lowest_prob + second_lowest_prob;
% TO SEE WHAT THIS DOES UNCOMMENT THIS CODE
% disp('The tree of sets and their probabilities:')
% for set_index = 1:length(set_probs)
% disp([num2str(set_probs(set_index)),' ', num2str(set_contents{set_index})]);
% end
end
%
% disp('-------------------------------------------------------------------------');
% disp('Huffman Tree:');
% disp('Letter|ASCII Value|Probability|Binary Code');
% for index = 1:length(codes)
% % ...display its bits in reverse order
% disp([num2str(char(asciiVals(index))), ' ', num2str(asciiVals(index)), ' ', num2str(probs(index)),' ',num2str(codes{index}(length(codes{index}):-1:1))]);
% end
%
%
%
% end

Answers (1)

Walter Roberson
Walter Roberson on 2 Mar 2013
for sK = 1 : numel(set_contents); disp(set_contents{sK}); end
There is no simple command to print out the contents of an entire cell array unless you are certain that the cells are all numeric and all the same size (in which case you can use cell2mat())
  1 Comment
James
James on 5 Mar 2013
hmm... well, all of my cells are of different sizes. Basically, I just need to be able to use that data. is there a way that I could save the codes cell array to a .mat file? And then perhaps the ascii values and the probabilities to a .txt file?

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!