Take the 2-minute tour ×

if possible please let me know that how I can read different text files in Matlab . considering that there is 33 txt files that every one should process. it is my code which has error. :(

 textFilename = cell(1,33);
        id = cell(1,33);
        for k=1:33;
        textFilename{k} = fullfile('C:\Users\Desktop\SentimentCode\textfiles',['file' num2str(k)     '.txt']);
        id{k} = fopen(textFilename{k},'rt'); 
        str{k} = textscan(id{k},'%s%s'); 

    str(str == '.') = '';
    str(str == '_') = '';
    str(str == '-') = '';

% Remove numbers from text
    T =regexprep(str, '[\d]', ' ');                                  

  and my error is :   ??? Undefined function or method 'eq' for input arguments of type 'cell'.

Error in ==> Untitled9 at 23
share|improve this question
what error do you get? –  Marcin May 31 at 1:21
many thanks for your reply .. I edited my post and add some lines more and the errors I appreciate you for your answering –  Asana May 31 at 11:27
and I used of fclose(id{k}) at the end of my code . –  Asana May 31 at 11:28
and the explanation of the error also is : The size of the indicated variable or array appears to be changing with each loop iteration. –  Asana May 31 at 12:17

2 Answers 2

up vote 0 down vote accepted

In your current edit your error seem more directed to the removal of . - and _ characters. The == comparasion works better with character strings while textscan returns a cell

Instead of


try using


to replace all at once (the '\.' is needed as '.' is a special character). This works on cellstrings so depending on how deep your cell structure goes you might need to call it within a for loop, str{k},str{k}{1}, str{k}{i} etc... An alternative could be to look at cellfun or/and strjoin... depending on how your data are arranged in the files.

share|improve this answer

Just by looking at the example code:

extFilename{k} = fullfile(..); 

should be

textFilename{k} = fullfile(...);

Also it is good idea to close the files after you read them: fclose(id{k})

share|improve this answer

Your Answer


