MATLAB: A problem while splitting a text input with regexp

MATLABregexpstring split

I have a text file with the input as
sammy yo yo
yoyo with you
Samyukta
and I tried the following code to put each word into an element of an array.
fid = fopen('test4.txt');
table = fscanf(fid,'%c');
table2 = regexp(table,'\n','split');
this means that when I refer to table2{1}, it returns 'sammy yo yo' then I split every line individually with strsplit and ' ' (whitespace) as the delimiter. Therefore, when I refer to table2{1}{2} , it returns 'ýo'. But, the last word of every line has more number of letters than appears i.e. size(table2{1}{2},2) = 3 rather than 2. But when I strcmp it with '\n' and ' ' or any other thing, it returns 0. So now I don't know what to do.

Best Answer

>> fprintf('%d,', table) ; fprintf('\n') ;
115,97,109,109,121,32,121,111,32,121,111,13,10,121,111,121,111,32,119,105,
116,104,32,121,111,117,13,10,83,97,109,121,117,107,116,97,13,10,
As you can see, at the end of each line, there are 13 (carriage return: '\r') and 10 (new line: '\n').
If you just want to split words, why don't you split using REGEXP only with a pattern which matches whitespaces? For example:
>> buffer = fileread('test4.txt') ;
>> words = regexp(buffer, '\s+', 'split')
words =
'sammy' 'yo' 'yo' 'yoyo' 'with' 'you' 'Samyukta' ''
with this, you would just have to delete the last cell when empty (which happens when your file ends with '\r\n'), and you would be done.