How to read .txt file and convert the characters of .txt into cell formation

1 vue (au cours des 30 derniers jours)
Shoaib Mirza
Shoaib Mirza le 19 Oct 2019
alphabets = {'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' 'p' 'q' 'r' 's' 't' 'u' 'v' 'w' 'x' 'y' 'z' ' '}; % Alphabet vector
prob = [0.0642 0.0127 0.0218 0.0317 0.1031 0.0208 0.0152 0.0467 0.0575 0.0008 0.0049 0.0321 0.0198 0.0547 0.0632 0.0152 0.0008 0.0484 0.0514 0.0996 0.0228 0.0083 0.0175 0.0013 0.0164 0.0005 0.1686]; % Probability vector
[dict,avglen] = huffmandict(alphabets,prob)
sample=fopen('Sample.txt','w');
fprintf(sample,'%d');
fclose(sample);
samples = num2cell(sample)
disp('The Huffman code is');
huffmancode = huffmanenco(samples,dict)
disp('The symbols are:');
huffmandecode = huffmandeco(huffmancode,dict)
isequal(samples,huffmandecode)
I am trying to compress a .txt file using huffman, but I am getting this error eventhough the ,txt file contains the smalls alphabets as in the huffman dicionary.
The Huffman code is
Error using huffmanenco (line 93)
The Huffman dictionary provided does not have the codes for all the input signals.
Error in HMCtext (line 15)
huffmancode = huffmanenco(samples,dict)

Réponses (1)

Guillaume
Guillaume le 19 Oct 2019
You ought to read the documentation of the functions you use if you don't know how they work. fopen doesn't read a file. All it does is open a file. It returns a file identifier (an integer number) so that you can differentiate between files if you open several at once. So your samples is just a file identifier completely unrelated to the content of the file.
Once a file is open you actually need to read its content, with one of the many reading function such as textscan, fread, fgetl, fgets, etc.
However, unless the file format is fairly complex you would probably be better off using one of the high-level reading function such as readmatrix, readtable, or the older dlmread or csvread. None of these require you to fopen and fclose the file. They do that and the decoding for you so are much simpler to use.

Catégories

En savoir plus sur Large Files and Big Data dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by