Function handle invoking Java reports syntax error?

2 ビュー (過去 30 日間)
Ahmed Fasih
Ahmed Fasih 2012 年 3 月 6 日
Why does
d='.'; mylength = length(java.io.File(d).listFiles(org.apache.commons.io.filefilter.WildcardFileFilter('*.dat')))
work, but a function handle for input "d" fail with a syntax error?
length_func = @(d) length(java.io.File(d).listFiles(org.apache.commons.io.filefilter.WildcardFileFilter('*.dat')))
??? Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.
  1 件のコメント
Ahmed Fasih
Ahmed Fasih 2012 年 3 月 7 日
This following does work:
helper=@(f) f.listFiles(org.apache.commons.io.filefilter.WildcardFileFilter('*.dat'))
length_func = @(d) length(helper(java.io.File(d)))

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

回答 (1 件)

Laurens Bakker
Laurens Bakker 2012 年 3 月 7 日
Hi Ahmed,
anonymous functions are not allowed to store any data, and accessing a field of java.io.File(d) probably implicitly does this.
Cheers,
Laurens

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by