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 件のコメント

Image Analyst
Image Analyst 2014 年 2 月 13 日
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
Anuj Anand
Anuj Anand 2014 年 2 月 13 日
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

サインインしてコメントする。

 採用された回答

Jos (10584)
Jos (10584) 2014 年 2 月 13 日

0 投票

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 件のコメント

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

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeExternal Language Interfaces についてさらに検索

タグ

質問済み:

2014 年 2 月 13 日

コメント済み:

2014 年 2 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by