Error using function regexp

조회 수: 13 (최근 30일)
ely may
ely may 2015년 12월 1일
답변: Sean de Wolski 2015년 12월 1일
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
Adam
Adam 2015년 12월 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개)

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

카테고리

Help CenterFile 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