Wednesday, November 4, 2015

[Trouble shooting] [MATLAB] Error using unique function; Error using cell/unique. Input A must be a cell array of string

Problem

If the input (A) of unique function contains non-string components, an error occurs.

Check whether the input A contains non-string components or not:

>> A = {'hello';'world';'hello';'young'};
>> unique(A(:,1))
ans = 
    'hello'
    'world'
    'young'



However if the A contains non-string components as like:

>> A = {'hello';[];'hello';'young'};

iscellstr(A(:,1))
ans =
      0



Solution

>> find(cellfun(@(x)~ischar(x),A))

ans =
      2


idxNonString = find(cellfun(@(x)~ischar(x),A));
A(idxNonString,1) = [];
unique(A(:1));

No comments:

Post a Comment