MATLAB: For-Loop with Regexp results in empty elements, not just the matches? How to loop only the matches

regexp loop

The output of regexp results in empty cells, if there is no match. To circumvent this I added the 'isempty' – to allow my code just check the matches. Is there a way to merge the regexp with isempty?
For example, the regexpi(varargin,'^.+(\.ppt|\.pptx)$','match') will give:
{1×1 cell} {}
for test = regexpi(varargin,'^.+(\.ppt|\.pptx)$','match')
if ~isempty(cell2mat(test{1}))
boOutput = cell2mat(test{1});
end
end

Best Answer

>> varargin = {'test.ppt','blah.pptx','false.txt'};
>> C = regexpi(varargin,'^.+\.(ppt|pptx)$','match','once');
>> for k = find(~cellfun('isempty',C))
disp(C{k})
end
test.ppt
blah.pptx
Note that I use the regexpi option 'once', which reduces the cell array nesting by only matching the regular expression pattern once. This then reduces the task to a simple check if the cells are empty.