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 Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by