anonymous function returning updated length of an array - matlab -


i'm trying write simple anonymous function returns length of array

>> a=[1 2 3];  >> f = @() length(a);  >> f()     3  >> = [1 2 3 4];  >> f()     3 

is possible write function returns length of updated array every time it's called?

an ugly method accomplish want

global a; = [1 2 3]; f = @() eval('global a; length(a)') f() = [1 2 3 4]; f() 

i'm compelled recommend against type of code relies on both globals , calls eval, both of should avoided when possible.

a better method pass in a argument function

a = [1 2 3]; f = @(x) length(x) f(a) = [1 2 3 4]; f(a) 

or, because in case calling f identical calling length, there's no reason use anonymous functions @ all.

a = [1 2 3]; length(a) = [1 2 3 4]; length(a) 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -