In MATLAB R2020b I have the following code:
f=@(x) x.^2;
y=2;
g=@(x) f(x).*y
and the output is
g = function_handle with value: @(x)f(x).*y
But y = 2, so I would expect the output to be @(x)f.*2. Is this how its supposed to work or a bug? And can I display it as f.*2 rather than f.*y?
When you create the function handle
g = @(x) f(x).*y, the current values of the variablesfandyget "frozen" intog's definition, as discussed in the documentation.To inspect the actual values of
fandythatguses, you can callfunctionsas follows:Specifically, see the
workspacefield: