Ben Hathaway
Ben Hathaway

Reputation: 356

Looping through documents in matlab

I am attempting to loop through the variable 'docs' which is a cell array that holds strings, i need to make a for loop that colllects the terms in a cell array and then uses command 'lower' and unique to create a dictionary.

Here is the code i've tried sp far and i just get errors

docsLength = length(docs);

for C = 1:docsLength
   list = tokenize(docs, ' .,-');
   Mylist = [list;C];
end

I get these errors

Error using textscan
First input must be of type double or string.
Error in tokenize (line 3)
    C = textscan(str,'%s','MultipleDelimsAsOne',1,'delimiter',delimiters);
Error in tk (line 4)
   list = tokenize(docs, ' .,-');

Upvotes: 0

Views: 114

Answers (1)

nkjt
nkjt

Reputation: 7817

Generically, if you get an "must be of type" error, that means you are passing the wrong sort of input to a function. In this case you should look at the point in your code where this is taking place (here, in tokenize when textscan is called), and doublecheck that the input going in is what you expect it to be.

As tokenize is not a MATLAB builtin function, unless you show us that code we can't say what those inputs should be. However, as akfaz mentioned in comments, it is likely that you want to pass docs{C} (a string) to tokenize instead of docs (a cell array). Otherwise, there's no point in having a loop as it just repeatedly passes the same input, docs, into the function.

There are additional problems with the loop:

Mylist = [list; C]; will be overwritten each loop to consist of the latest version of list plus C, which is just a number (the index of the loop). Depending on what the output of tokenize looks like, Mylist = [Mylist; list] may work but you should initialise Mylist first.

Mylist = [];

for C = 1:length(docs)
   list = tokenize(docs{C}, ' .,-');
   Mylist = [Mylist; list];
end

Upvotes: 2

Related Questions