Filter löschen
Filter löschen

Convert string function to function handle with struct parameters

1 Ansicht (letzte 30 Tage)
Hyungjin Choi
Hyungjin Choi am 20 Apr. 2019
Kommentiert: per isakson am 26 Apr. 2019
Hello, I am trying to define function handle with the following structure:
odefunc = @(t,x) myfunc(t,x,param)
where param is a struct with the following fields: param.a = matrix; param.b = vector; param.c = number; etc. This function will be used for solving ode:
[t,x] = ode45(odefunc, tspan, init);
What I am trying to do is convert string function name to actual function handle to be used in solving the ode. The following is what I tried:
===============================================
string_name = 'myfunc';
odefunc = DefineMyFunction(string_name,param);
[t,x] = ode45(odefunc,tspan,init);
function odefunc = DefineMyFunction(string_name,param)
func = str2func('@(t,x) string_name(t,x,param)');
===============================================
Of course, it is not working because struct "param" is not fed into myfunc properly. I found many good examples in case that param is not struct but a simple single variable . But, how can I construct functiona handle with struct?
Thank you for your help in advance.
  4 Kommentare
Walter Roberson
Walter Roberson am 26 Apr. 2019
myfunc = str2func(string_name);
func = @(t,x) myfunc(t, x, param);

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Produkte


Version

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by