Convert string function to function handle with struct parameters

1 回表示 (過去 30 日間)
Hyungjin Choi
Hyungjin Choi 2019 年 4 月 20 日
コメント済み: per isakson 2019 年 4 月 26 日
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 件のコメント
Walter Roberson
Walter Roberson 2019 年 4 月 26 日
myfunc = str2func(string_name);
func = @(t,x) myfunc(t, x, param);
per isakson
per isakson 2019 年 4 月 26 日

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

製品


リリース

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by