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

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일

1 개 추천

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일

0 개 추천

Hi
Actually I want to get the function from user and save it
not to be defined as f{i} = @(x) x*i;

댓글 수: 12

Jan
Jan 2013년 4월 17일
Please do not post details of the question as an answer, but inster it in the question, where readers expect them.
What does "get the function from user" exactly mean? It is impossible to guess this reliably.
Friedrich
Friedrich 2013년 4월 17일
편집: Friedrich 2013년 4월 17일
I think what he wants to do is the same as discussed here:
Still dont use inline, use eval to generate the function handle. See me updated first post.
Masoud Ghanbari
Masoud Ghanbari 2013년 4월 18일
Thanks Dear Friedrich
and what if i want to get the function from user by 2 variables like
f(i) = @(x,y) x+y
As long the user enters a valid matlab expression you are fine to eval it.
>> user_input = '@(x,y) x+y'
user_input =
@(x,y) x+y
>> my_fcn = eval(user_input)
my_fcn =
@(x,y)x+y
>> my_fcn(3,10)
ans =
13
>>
Why we got error while changing 'inputdlg' to 'input' below ???
f = cell(2,1);
for i=1:2
tmp = inputdlg('enter function handle')
f{i} = eval(tmp{:})
end
And why error when user inputs '@(x,y)x+y' at inputdlg
number=input(' How Many Conditions Do You Have ? ');
f = cell(number,1);
for i=1:number
user_input = inputdlg('Enter The function :');
f{i} = eval(user_input);
end
Friedrich
Friedrich 2013년 4월 19일
input returns a character array wheras inputdialog gives back a cell.
This @(x,y)x+y should give any error. What error do you get?
When I Run
number=input(' How Many Conditions Do You Have ? ');
f = cell(number,1);
for i=1:number
user_input = inputdlg('Enter The function :');
f{i} = eval(user_input);
end
inputdlg appear and when i enter
@(x,y)x+y
i will face error at this line below
f{i} = eval(user_input);
Iman Ansari
Iman Ansari 2013년 4월 19일
편집: Iman Ansari 2013년 4월 19일
Hi. Use this:
f{i} = eval(user_input{1});
Masoud Ghanbari
Masoud Ghanbari 2013년 4월 19일
Hi Iman
It Does not work...
Friedrich
Friedrich 2013년 4월 19일
Dude ;) Post your intput and the error message. A simply does not work wont help.
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
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일

0 개 추천

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

카테고리

도움말 센터File Exchange에서 Function Creation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by