MATLAB: How to create a local function during the code run

local functionsode

Hello, everyone!
I have a question about opportunity of creating local functions during the code run.
For example I have this code:
function diffprobe
x0=[1 1];
for i=1:2
a=strcat('@odeEvent', num2str(i));
str = str2func(a);
options(i)=odeset('Events', str, 'RelTol',1e-9,'AbsTol',1e-9);
[~,~,TE,XE,IE]=ode45(@System, [0 inf],x0, options(i));
end
function [value, isterminal, direction]=odeEvent1(~,x)
value=[x(1)];
isterminal=[1];
direction=0;
function [value, isterminal, direction]=odeEvent2(~,x)
value=[x(2)];
isterminal=[1];
direction=0;
function RPF=System(~,x)
RPF=[x(2);...
-x(2)-1];
It is work right, but i have an interest to somehow create local functions odeEvent1 and odeEvent2 during the cycle, because of I need to generalize the method on high order systems so the cycle might go not from 1 to 2, but from 1 to n, and obviously I don't want to rewrite the code every time, when I try the new system.

Best Answer

Dynamically creating a function name is not a recommended coding practice. Following shows how to do such a thing more efficiently
function diffprobe
x0=[1 1];
for i=1:2
options(i)=odeset('Events', @(t,x) odeEvent(t,x,i), 'RelTol',1e-9,'AbsTol',1e-9);
[~,~,TE,XE,IE]=ode45(@System, [0 inf],x0, options(i));
end
function [value, isterminal, direction]=odeEvent(~,x,i)
value=[x(i)];
isterminal=[1];
direction=0;
function RPF=System(~,x)
RPF=[x(2);...
-x(2)-1];