MATLAB: How to read from a file into a char array

char arraycounting charsMATLABreading from file

I have a large text file, and I need to calculate the number of times each individual letter occurs in the file. The easiest way I can think of to do that would be to have an array where each entry is a single char from the file, then run an array function on the whole thing and sum the number of times each letter is found. However I am having trouble getting the text from the file into a char array. I have tried using fileread, which reads the entire file to a single entry in a string array, and I have tried using textscan, which reads the file into a cell array split up by words. Does anyone know if I can just get the file straight into a char array?

Best Answer

Try
str = fileread( filespec );
num = double( str );
nch = histc( num, [1:255] ); % fix [32:255]
A little test - added later
>> char( find( histc( double('abcd1234'), [1:255] ) ) )
ans =
1234abcd