Search code examples
stringmatlabcell-array

Input must be a string MATLAB ERROR when using cellstr(sentiment)


My sentiment variable is cell class put when I try parse it to the cellstr fucntion it throws the following error...

cellstr(sentiment) Error using cellstr (line 32) Input must be a string.

I am trying to use the unique command but it throws this error... Error using cell/unique (line 85) Input A must be a cell array of strings.

When I open the variable there is a single column with sentiments positive, negative, neutral but it looks like each cell has a sub-cell. Could this be a reason for the errors? If so how would I correct this ?

I take a cell array that contains some unnecessary characters so i filter out for the relevant word using:

for i= 1:length(sentdate)
s=sentiment{i};
sentiment{i}={s(15:22)};

s2=date{i};
date{i}={s2(17:26)};

Thanks in advance


Solution

  • You don't need the curly brackets around whatever you're assigning into a cell. The assignment is exactly symmetric to reading out the value:

    s = sentiment{i};
    sentiment{i} = s(15:22);
    

    Surrounding a value with {} actually creates a cell array, which is why you appear to have a subcell. To help understand this difference between () and {}, try this:

    sentiment(i) = {s(15:22)};
    

    Same result! By using () to index sentiment, we're not dereferencing the cell contents. So then the value to be assigned must itself be a cell.