0

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'); 

    end                                                      
    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
str(str == '.') = '';
Roze
  • 23
  • 1
  • 7

2 Answers2

0

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})

Marcin
  • 215,873
  • 14
  • 235
  • 294
0

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

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

try using

regexprep(str,'(\.|-|_)','')

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.

NLindros
  • 1,683
  • 15
  • 15