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 comentarios

Image Analyst
Image Analyst el 13 de Feb. de 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

Iniciar sesión para comentar.

 Respuesta aceptada

Jos (10584)
Jos (10584) el 13 de Feb. de 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 comentarios

Anuj Anand
Anuj Anand el 13 de Feb. de 2014
Many Thanks. This is brillient... the 2nd version is 7 times faster than the 1st version. Cheers.
Jos (10584)
Jos (10584) el 13 de Feb. de 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 el 13 de Feb. de 2014
Thanks Jos, I did reaslise that. I did read up on it a little bit. Thanks again...

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre External Language Interfaces en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 13 de Feb. de 2014

Comentada:

el 13 de Feb. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by