How to call function repeatedly using arrays as variables?

10 views (last 30 days)
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
Duc Le
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.
Steven Lord
Steven Lord on 27 Nov 2022
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)
ans = 'function_handle'
g = @(x) x.^2; % anonymous function
class(g)
ans = 'function_handle'
h = @why; % handle to a MATLAB function file why.m
class(h)
ans = 'function_handle'
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."

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 15 Nov 2022
Edited: James Tursa on 15 Nov 2022
There are other ways do to this, but to answer your question about how to turn the code into a loop, use indexing based on the number of elements of your array. E.g., added the for-loop and the (k) indexing:
Vout = zeros(size(Va)); % pre-allocate
for k=1:numel(Va) % added the for-loop
if Va(k) > Vb(k)
Vout(k) = Vcc; % add semi-colons so the result doesn't print to screen
elseif Va(k) == Vb(k)
Vout(k) = 0;
else
Vout(k) = Vee;
end
end % end for-loop
This assumes Va and Vb have the same number of elements. And put the return statement outside the loop.
  1 Comment
Liam Hoyle
Liam Hoyle on 15 Nov 2022
tysm! This is much more elegant than the code I was trying to write using arrayfun and the helper function!

Sign in to comment.

More Answers (0)

Categories

Find more on Variables in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!