Can someone explaine why my function handle does not work? - matlab

Newbe here so please don't be too harsh.
I have these three functions, and each has it's own file:
format long
rd = #(x) runden(x,L);
function y = runden(x,L)
y = (round(x*10^L))/10^L;
endfunction
format long
function z = add(x,y,rd)
z = rd(rd(x)+rd(y));
endfunction
format long
function z = mult(x,y,rd)
z = rd(rd(x)*rd(y));
endfunction
as you see I want to use a function handle, so I can use round in the bottom two functions.
The functions have to be in their own files (wasn't a problem so far in matlab).
My Syntax is off, but I can't find a tutorial that handles function handles with two variables.

If I understand, the function handle rd lives only in the first script, and the other scripts can't access it.
If you will name the first file (function) as runden.m, and define rd in the other functions and not in the first one, they will have access to it.
And two notes- don't override preserved function names as add, and use end instead of endfunction.
I would suggest to arrange the functions like that:
% file 1, named `runden.m`
function y = runden(x,L)
format long
y = (round(x*10^L))/10^L;
end
% file 2, named `ad.m`
function z = ad(x,y,L)
format long
rd = #(x) runden(x,L);
z = rd(rd(x)+rd(y));
end
% file 3, named `mult.m`
function z = mult(x,y,L)
format long
rd = #(x) runden(x,L);
z = rd(rd(x)*rd(y));
end

Related

Body of this Matlab function works, but not the function itself (interp1 error)

I've written the following piece of subcode (with parameters commented) for an Euler policy iteration algorithm. When I try to run the body of the function (everything below global) for say, a1 = 1, it works, and returns a scalar. However, when I call the function as euler_diff_test(1), I get an error. (Pasted below)
function diff = euler_diff_test(a1)
%the following comments are example parameters. They are in the global line originally.
% r = 0.2, a = 0.5, y = 1.1, a_grid = linspace(0.5,7,100)
%policy_guess = zeros(2,N);
%policy_guess(1,:) = 0.3*a_grid;
%policy_guess(2,:) = 0.3*a_grid;
% M = zeros(2,2); %M for markov transition kernel
% M(1,1) = p;
% M(2,2) = p;
% M(2,1) = 1-p;
% M(1,2) = 1-p;
% j = 1
global r a y a_grid policy_guess M j;
c = (1+r)*a + y - a1; %consumption formula
if c<=1e-02 %don't care about consumption being negative
diff = 888888888888888888888;
else
policy_func = interp1(a_grid', policy_guess', a1, 'linear');
diff = 1/c - beta*(1+r)*(1 ./ policy_func)*M(j,:)';
end
end
Error Reads:
Any help is much appreciated!
The problem is that you dont understand globals nor how they work!
You seem to be doing something like:
N=100; p=0.1;
r = 0.2, a = 0.5, y = 1.1, a_grid = linspace(0.5,7,100)
policy_guess = zeros(2,N);
policy_guess(1,:) = 0.3*a_grid;
policy_guess(2,:) = 0.3*a_grid;
M = zeros(2,2); %M for markov transition kernel
M(1,1) = p;
M(2,2) = p;
M(2,1) = 1-p;
M(1,2) = 1-p;
euler_diff_test(1)
And this is causing the error you show. Of course it is!
First, you need to learn what a global is and what worskpaces are. Each fucntion has its own worskpace or "scope". That means that only variables defined within the workspace are visible by the function itself.
A global variable is one that exist for all workspaces, and everyone can modify it. You seem to want all those variables defined outside the function, inside your function. But realise! when the variables are defined, they are not global. The function starts, and in its first line, it does only know about the existence of a1. Then, later, you define a bunch of variables as global, that the function did not know about. So what does the function do? just create them empty, for you.
If you want your the variables that you create in the main script scope to be global, you need to declare them as global then, not inside the function. So cut your line global ... from the fucntion, and put it on top of the script where you declare all your variables, i.e. on top of
% here!
N=100; p=0.1;
...
in my example.
Now, the important stuff: Global variables are bad. When you have globals, you don't know who modifies, and its super easy to lost track of what is happening to them, because every function that uses a variable a will modify the global a, so its a pain to debug. Almost no one uses globals because of this. The best way is to pass them to the function as input, i.e. define your function as:
function diff = euler_diff_test(a1,r, a, y, a_grid, policy_guess, M, j)

How to combine function with other codes in one .m fine (Matlab)

clear all;
close all
clc
e = .25;
%fun = #find_root;
z = fzero(fun,1)
yy = z+.5^2*z/e-z^3/e
%=================================
function y = find_root(x)
y1 = x+0.5^2*x/e-x^3/e;
y = -e*x + e*y1 +.5^2*y1-y1^3
end
It can work if I separate two parts in different .m file of the above code. However, when I
combine them together, Matlab shows:
Error: File: find_root.m Line: 11 Column: 14
Function with duplicate name "find_root" cannot be defined.
Since I want to set e from 0 to 1 in for loop and I cannot add parameter in the following way
z = fzero(fun(x,e),1)
that is why I have to combine both parts in ONE .m file.
How to fix it?
Okay, so there is a few things wrong here. Firstly with your error:
Error: File: find_root.m Line: 11 Column: 14 Function with duplicate name "find_root" cannot be defined.
This is caused when you give your file the same name as a function contained in the script. I suggest you change it to something else (e.g. calc_yy.m). Other than that you should define your function as a function handle with your desired input (fun = #(x)find_root(x,e); is a function handle with input x). Something else to watch out for is including e as a parameter for your function. If you do not include e as a function parameter in the definition function y = find_root(x,e) and the function handle fun = #(x)find_root(x,e);, then the e you defined earlier will be out of scope within the function. The following code worked just fine for me (saved as test.m):
%% Script
clear all
close all
clc
e = .25;
fun = #(x)find_root(x,e);
z = fzero(fun,1);
yy = z+.5^2*z/e-z^3/e;
%% Functions
function y = find_root(x,e)
y1 = x+0.5^2*x/e-x^3/e;
y = -e*x + e*y1 +.5^2*y1-y1^3;
end
Good luck with your future MATLAB endeavours and don't ever feel silly for mistakes like this, we have all made them at some point!

Is it possible to change an anonymous function keeping the workspace?

What I would like to be able to do is programmatically change an anonymous function for example by changing all the plus signs to multiplication signs in the function. This example can in many cases this can be done as follows:
function f2 = changefunction(f1)
fs = func2str(f1);
fs(fs=='+') = '*';
f2 = str2func(fs);
end
But consider the example
f = #(x) x+5;
a = 5;
g = #(x) x+a;
Both f and g will be anonymous functions that adds 5 to whatever you plug into it; however only f will be changed correctly by the changefunction function, whereas g will be changed into a function that will err on any input.
So my question is is it possible to extract the workspace from the function handle and retain it in the new function handle created? I need to do it programmatically and preferably without using the built-in function functions!
One naive implementation is to replace str2func with eval so you are not running into str2func's roadblock of not allowing access to local variables. We can use functions to obtain the workspace information for the input function handle.
For example:
a = 5;
f = #(x) x+a;
finfo = functions(f)
Yields:
finfo =
struct with fields:
function: '#(x)x+a'
type: 'anonymous'
file: 'X:\testcode-matlab\testcode.m'
workspace: {[1×1 struct]}
within_file_path: 'testcode'
Where workspace is a cell array containing a structure (come on MathWorks...) containing all of the variables in your function handle's namespace:
>> wspace = finfo.workspace{1}
wspace =
struct with fields:
a: 5
Using this functionality, the naive solution is to loop through the variables in this workspace, assign them in the namespace of changefunction, then use eval to generate the new function handle.
For example:
function f2 = changefunction_new(f1)
tmp = functions(f1);
workspacevars = tmp.workspace{1};
varnames = fieldnames(workspacevars);
for ii = 1:length(varnames)
evalstr = sprintf('%s = %d;', varnames{ii}, workspacevars.(varnames{ii}));
eval(evalstr);
end
fs = func2str(f1);
fs(fs=='+') = '*';
f2 = eval(fs);
end
Here I'm assuming that the variables are going to be strictly numeric. You can add logic to check the class of the data to be generated if this is not always the case.
With this we have:
a = 5;
g = #(x) x+a;
test1 = changefunction(g);
test2 = changefunction_new(g);
>> g(1)
ans =
6
>> test1(1)
Undefined function or variable 'a'.
Error in testcode>#(x)x*a
>> test2(1)
ans =
5
All that being said, the best solution really is to just explicitly define your function handles. It may be a pain but it's much easier to understand and debug.
A few caveats:
Because eval arbitrarily executes all code passed to it, it can be a very dangerous function that must be used with care.
The documentation for functions warns against using it programmatically, so take care to check behavior as MATLAB versions change:
Use the functions function for querying and debugging purposes only.
Note: Do not use functions programmatically because its behavior could change in subsequent MATLAB® releases.
One possible way to do this is to save the function handle to a .mat file (using the -v7.3 flag so that it creates an easily-modifiable HDF5 file), modify the struct within the file that contains the workspace data for the anonymous function (using the HDF5 tools built into MATLAB), and then load the anonymous function again from the file.
Here is a little function which does exactly that (and it works for relatively simple variable types)
function result = modifyfunc(f, varname, value)
% modifyfunc - Modify the workspace of an anonymous function
%
% INPUTS:
% f: Function Handle, Anonymous function to modify
% varname: String, Name of the variable to modify
% value: Data to replace the specified variable
% If the value is a struct, recursively modify the function handle
if isstruct(value)
fields = fieldnames(value);
result = f;
% Modify each field separately
for k = 1:numel(fields)
% Append the fieldname to the variable name and modify
name = [varname, '.', fields{k}];
result = modifyfunc(result, name, value.(fields{k}));
end
return;
end
% Write the anonymous function to an HDF5 file
fname = tempname;
save(fname, 'f', '-mat', '-v7.3');
% Replace any "." in the variable name with "/" to construct the HDF5 path
varname = strrep(varname, '.' , '/');
% Now modify the data in the file
h5write(fname, ['/#refs#/e/' varname], value);
% Load the modified function handle from the file
result = load(fname, '-mat');
result = result.f;
% Remove the temporary file
delete(fname);
end
And you can use it like:
a = 1;
b = struct('field', 2);
f = #(x)disp(a + b.field + x);
f(10)
% 13
f2 = modifyfunc(f, 'a', 2);
f2(10)
% 14
f3 = modifyfunc(f2, 'b.field', 3);
f3(10)
% 15
b.field = 4;
f4 = modifyfunc(f3, 'b', b);
f4(10)
% 16
Some caveats include:
The replacement data must be the same size as the original data
This relies upon the format of the .mat file which for anonymous functions is completely undocumented so it could fail in future releases.
This currently doesn't work for variables in the function workspace that are cell arrays.

How to generate a function inside a script file in MATLAB

I am looking for a way to write a script file in MATLAB that generates a sequence of functions and save them each in a separate m-file.
More precisely, suppose you have a script file and you want to generates N different functions; something like this:
for i=1:N
Step 1: Do some symbolic calculation and find the expression of fi
Step 2: Define the function fi=fi(...)
Step 3: Save the the function in an m-file say fi.m
end
I myself have not found any clue so far. Here is two questions that if answered, such code then follows straightforwardly:
1. How to define a function in a script file and automatically save it in an m-file:
Let say y=f(x)=x^2 is a symbolic relation calculated in a script file; how to automatically generate a function for it and save it in an m-file
2. How to use a string as a file name, function name, etc: Suppose you are creating an string "fi" in a loop for every i, and you want to use this string as a function name
Thanks for your help in advance
https://www.mathworks.com/help/symbolic/matlabfunction.html#bul5mb5-1
If you want to convert symbolic functions to MATLAB function files, MATLAB has a function (called matlabFunction) that does exactly that.
Example from that page:
syms x y z
r = x^2 + y^2 + z^2;
f = matlabFunction(log(r)+r^(-1/2),'File','myfile');
Which creates 'myfile.m':
function out1 = myfile(x,y,z)
%MYFILE
% OUT1 = MYFILE(X,Y,Z)
t2 = x.^2;
t3 = y.^2;
t4 = z.^2;
t5 = t2 + t3 + t4;
out1 = log(t5) + 1.0./sqrt(t5);

Use MATLAB's 'keyPressFcn' in a simple program

I am trying to use the 'KeyPressFcn' in a normal MATLAB script, but I am having problems. I can use it nicely WITHIN a function, (like here), but I would like to use it in a normal script like so.
My simple script is:
%Entry Point
clear all
N = 100;
x = randn(1,N);
figHandle = figure(1);
clf(figHandle);
set(figHandle, 'KeyPressFcn', myFunction(~, eventDat,x,N))
Here is the function 'myFunction' that sits in the same directory:
function myFunction(~, eventDat,x,N)
mean = sum(x)/N;
disp(mean);
key = eventDat.Key;
disp(key);
end
Now, if I run this, it does not work, because, (I suspect), something is wrong with the way I am calling myFunction, but I cannot figure out what the problem is exactly, since I am a noob at using KeyPressFcn. Help would be appreciated for this problem. Thanks!
You need to do it through anonymous functions:
In script file, for example called test.m:
%Entry Point
clear all
N = 100;
x = randn(1,N);
figHandle = figure(1);
clf(figHandle);
set(figHandle, 'KeyPressFcn', ...
#(fig_obj , eventDat) myFunction(fig_obj, eventDat, x, N));
In a file called myFunction.m in the same folder as test.m
function myFunction(~, eventDat, x, N)
mean = sum(x)/N;
disp(mean);
key = eventDat.Key;
disp(key);
How to return value from myFunction?
There are few ways of doing this. It depends on what u want to do. But quickly you could use mutable variables for this, such as, containers.Map. This is one example of ding this. The returned variable is newN.
In script file, for example called test.m:
%Entry Point
clear all
N = 100;
x = randn(1,N);
% this map will store everything u want to return from myFunction.
returnMap = containers.Map;
figHandle = figure(1);
clf(figHandle);
set(figHandle, 'KeyPressFcn', ...
#(fig_obj , eventDat) myFunction(fig_obj, eventDat, x, N, returnMap));
% wait till gui finishes in this example.
waitfor(figHandle);
newN = returnMap('newN');
% display newN
newN
In a file called myFunction.m:
function myFunction(handle, eventDat, x, N, returnMap)
mean = sum(x)/N;
disp(mean);
key = eventDat.Key;
disp(key);
newN = 435;
returnMap('newN') = newN;