catchFolders = {'iptCatch','imtCatch','iftCatch','','junk','iitCatch'}; t = strfind(catchFolders(:),'Catch'); for k = length(t):-1:1 if isempty(t{k}), catchFolders(k)=[]; end end catchFolders

2 commentaires

Image Analyst
Image Analyst le 13 Fév 2014
What is your question? I mean, other than "how do I format my code so that it doesn't all show up on one line?" which will be solved once you read this: http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
Sorry. My question was to remove the for-loop from the above code. Jos answered it beautifully. many thanks again
if true
% code
catchFolders= {'iptCatch','imtCatch','iftCatch','','junk','iitCatch'};
t= strfind(catchFolders(:),'Catch');
for k= length(t):-1:1
if isempty(t{k}),
catchFolders(k)=[];
end
end
end

Connectez-vous pour commenter.

 Réponse acceptée

Jos (10584)
Jos (10584) le 13 Fév 2014
You want a one-liner? Try these:
catchFolders = {'iptCatch','imtCatch','iftCatch','','junk','iitCatch'} % data
catchFolders1 = catchFolders(~cellfun('isempty',strfind(catchFolders(:),'Catch')))
catchFolders2 = regexp(sprintf('%s ',catchFolders{:}),'\w*Catch\w*','match')

3 commentaires

Anuj Anand
Anuj Anand le 13 Fév 2014
Many Thanks. This is brillient... the 2nd version is 7 times faster than the 1st version. Cheers.
Jos (10584)
Jos (10584) le 13 Fév 2014
You're welcome. But not that the second version is also a little more problematic than the first.
  1. more difficult to understand
  2. more prone to errors (especially when cells in catchFolders contain blanks)
  3. not so easy to modify (e.g. when the string to search for is variable)
Anuj Anand
Anuj Anand le 13 Fév 2014
Thanks Jos, I did reaslise that. I did read up on it a little bit. Thanks again...

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur External Language Interfaces dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by