フィルターのクリア

'inline' command at 'for' loop... can we?

29 ビュー (過去 30 日間)
Masoud Ghanbari
Masoud Ghanbari 2013 年 4 月 17 日
can we put 'inline' command at 'for' loop
I want to save several inline function at an array... Available?

採用された回答

Friedrich
Friedrich 2013 年 4 月 17 日
編集済み: Friedrich 2013 年 4 月 17 日
Hi,
yes it should be possible but its better to create some Anonymous Functions instead of using inline, e.g.
f = cell(10,1);
for i=1:10
f{i} = @(x) x*i;
end
The output of f looks a bit strange because it seems to be the same function handle all the time, but it isn't:
>> f{1}(1)
ans =
1
>> f{2}(1)
ans =
2
>> f{3}(1)
ans =
3
>> f{3}(4)
ans =
12
Or:
f = cell(2,1);
for i=1:2
tmp = inputdlg('enter function handle')
f{i} = eval(tmp{:})
end
And enter things like:
@(x)x^2
@(x)x-3
Make the call to eval fail proof!!
  1 件のコメント
Daniel Shub
Daniel Shub 2013 年 4 月 19 日
There is no need to hit it with the evil eval hammer, str2func seem perfectly designed for this purpose with hopefully a lot less side effects

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

その他の回答 (2 件)

Masoud Ghanbari
Masoud Ghanbari 2013 年 4 月 17 日
Hi
Actually I want to get the function from user and save it
not to be defined as f{i} = @(x) x*i;
  12 件のコメント
Masoud Ghanbari
Masoud Ghanbari 2013 年 4 月 20 日
Ok Dear Friedrich ... Please Just Do What Ever You Know... I'm Really Stucked At This
See Here:
https://www.youtube.com/watch?v=zQiAgVLXknI
Friedrich
Friedrich 2013 年 4 月 22 日
You need to zuse {} instead of () when you call eval. inputdlg gives back a cell. Also don't use i to index into it. This should work:
f{i} = eval(user_input{1})

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


Masoud Ghanbari
Masoud Ghanbari 2013 年 4 月 20 日
Ok Dear Friedrich ... Please Just Do What Ever You Know... I'm Really Stucked At This
See Here:
https://www.youtube.com/watch?v=zQiAgVLXknI

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by