How to call function repeatedly using arrays as variables?
Show older comments
I created a function for a comparator that works correctly, it looks like this:
function Vout = myComparator(Va, Vb, Vcc, Vee)
if Va > Vb
Vout = Vcc
elseif Va == Vb
Vout = 0
else Vout = Vee
return
end
Now I am trying to find multiple "Vout"s given two seperate arrays of Va and Vb values (edit: Vcc and Vee are constant). Then I need to graph these values versus Va, Vb, and the given time data. Omitting my helper function and list of values, my code looks like this:
% Compute Outputs
B = arrayfun(myComparator, Va, Vb);
% Plot Inputs and Outputs
plot(t, Va, t, Vb, t, B);
legend('Va', 'Vb', 'B');
xlabel('time(s)'); ylabel('amplitude'); title('Non-inverting amplifier output voltage');
When I have the code for myComparator at the bottom, it gives the error messages:
"Not enough input arguments. "
"Error in solution>myComparator (line 17) if Va > Vb"
"Error in solution (line 8) B = arrayfun(myComparator, Va, Vb)"
When the myComparator code is at the top, I get no output. I feel like I am highly overthinking this and can't seem to find the issue, unless I am misunderstanding arrayfun in the documentation. Adittionally, this assigment is being completed in a canvas portal so I can't really see the full error messages.
I was told by my professor to use arrayfun or a for loop. I'm definetley a novice at coding, any info will be appreciated.
5 Comments
You need to provide a function handle, not call the function (as you are doing):
B = arrayfun(@myComparator, Va, Vb);
% ^ define function handle
Note that your code will then throw an error because the 3rd and 4th function inputs are not provided.
Liam Hoyle
on 15 Nov 2022
Moved: Stephen23
on 15 Nov 2022
Stephen23
on 15 Nov 2022
"I'm not sure why Matlab documentation doesn't show the need for the "@" "
The ARRAYFUN documentation states that the first argument "... is a function handle to a function that takes one input argument and returns a scalar". All of the examples show anonymous functions, and these links are at the bottom of the page:
Duc Le
on 27 Nov 2022
I think you can use arrayfun with an anonymous function:
B = arrayfun(@(a,b) myComparator(a, b, Vcc, Vee), Va, Vb)
You'll have to define the value of Vcc and Vee first though or use actual numbers in their place in the anonymous function.
I think you can use arrayfun with an anonymous function:
Yes. An anonymous function is a function handle, just like a handle to a "named" function is a function handle.
f = @sin; % handle to a built-in function
class(f)
g = @(x) x.^2; % anonymous function
class(g)
h = @why; % handle to a MATLAB function file why.m
class(h)
From the documentation: "Many MATLAB® functions accept function handles as inputs so that you can evaluate functions over a range of values. You can create handles either for anonymous functions or for functions in program files. The benefit of using anonymous functions is that you do not have to edit and maintain a file for a function that requires only a brief definition."
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements 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!