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

read text file char array

This is probably trivial, but I have not found an easy way to convert a text file of n lines and m columns (sample attached) into a char array of the same shape. The only way that might work for me is an n-long for-loop of "fscanf", but I do not know n beforehand, and I figure there must be a simpler and faster vectorized command, which I ignore (I tried fileread and textread to no avail).

Best Answer

temp = regexp(fileread('sample_input.txt'), '\r?\n', 'split');
output = vertcat(temp{:});