How to call function repeatedly using arrays as variables?

조회 수: 13 (최근 30일)
Liam Hoyle
Liam Hoyle 2022년 11월 15일
댓글: Steven Lord 2022년 11월 27일
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
Duc Le
Duc Le 2022년 11월 27일
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 2022년 11월 27일
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."

댓글을 달려면 로그인하십시오.

채택된 답변

James Tursa
James Tursa 2022년 11월 15일
편집: James Tursa 2022년 11월 15일
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
Liam Hoyle
Liam Hoyle 2022년 11월 15일
tysm! This is much more elegant than the code I was trying to write using arrayfun and the helper function!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Variables에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by