Error using function regexp

10 views (last 30 days)
ely may
ely may on 1 Dec 2015
Answered: Sean de Wolski on 1 Dec 2015
Problem: in this code I want to obtain postfixes of semanticTrajCompact.Trajcompact; after I want count the combination of values 0-5 in them. I'm not able to solve why the function 'regexp' generates the error 'Error using regexp. All cells must be strings.'
for k=1:25
[matches(:,k), postfix(:,k)] = regexp(semanticTrajCompact(1,4).TrajCompact,sprintf('%d%d(.*)',digits{1}(k),digits{2}(k)),'match','once','tokens');
end
postfix(cellfun(@isempty,postfix)) = {''};
for k=1:25
matchcount = cell(size(digits{1}));
if ~isempty(postfix)
for i = 1:numel(matchcount)
matchstart = regexp(postfix, sprintf('%d.+?%d', digits{1}(k), digits{2}(k)));
matchcount{i} = cellfun(@numel,matchstart);
end
end
end
for idx=1:numel(matchcount)
matchcount{idx}=permute(matchcount{idx},[3,2,1]);
end
matchcount=cell2mat(matchcount);
valueNoZero=(matchcount~=0);
counter=sum(valueNoZero(:,:,:),3);
Postfix is a cell array 93% empty. How can I solve the problem? Maybe I need to filter out the non-string cells before passing them to regexp. But I don't know to filter out the non string cells and at the same time have the same structure of postfix [nx25]... I have tried horzcat/vertcat but this function put all values in the same vector.
  1 Comment
Adam
Adam on 1 Dec 2015
cellfun( @ischar, postfix )
will tell you which elements of your array are not strings.
postfix(cellfun(@isempty,postfix)) = {''};
in your code should have dealt with all the cases of empty cells and turned them into strings.

Sign in to comment.

Answers (1)

Sean de Wolski
Sean de Wolski on 1 Dec 2015
iscellstr(C)
Has to return true. The following should remove empty or non-character elements from the cell.
C = C(cellfun(@ischar,C));

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!