one line form of the follwing 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 catchFolders

2 Commenti

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

Accedi per commentare.

 Risposta accettata

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 Commenti

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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su MATLAB in Centro assistenza e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by