How to pass vectors as arguments for functions created using str2func ?
Show older comments
f=str2func('@(x,y,z) x+y+z');
w=[1,1,1];
y=f(w) % want to compute f(1,1,1) using vector x
This gives an error when I tried to compute y
Thanks in Advance for your help !!
5 Comments
% want to compute f(1,1,1) using vector x
The problem is that f(1,1,1) is NOT one vector, but three independent input arguments to f. Confusion about very basic MATLAB concepts like this is best helped by doing the introductory tutorials (which are highly recommended for all beginners):
And in particular this section:
Jos (10584)
on 6 Feb 2018
To add a simple logic: you have defined x but not y and z as inputs ...
Abhishek Panchal
on 6 Feb 2018
Edited: Abhishek Panchal
on 6 Feb 2018
f= @(w) sum(w);
w=[1,1,1];
y=f(w)
would do what you ask. str2func just obfuscates what you are doing by adding an extra un-needed layer.
If you want to do something more complicated than a sum on a vector input though that is complicated in an anonymous function like that given variable input lengths.
Abhishek Panchal
on 6 Feb 2018
Accepted Answer
More Answers (0)
Categories
Find more on Parallel Computing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!