Error using function regexp

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 个评论

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.

请先登录,再进行评论。

回答(1 个)

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

类别

帮助中心File Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by