MATLAB Answers

0

Function handle invoking Java reports syntax error?

Asked by Ahmed Fasih on 6 Mar 2012
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 Comment

This following does work:
helper=@(f) f.listFiles(org.apache.commons.io.filefilter.WildcardFileFilter('*.dat'))
length_func = @(d) length(helper(java.io.File(d)))

Sign in to comment.

1 Answer

Answer by Laurens Bakker on 7 Mar 2012

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

  0 Comments

Sign in to comment.