Coding an if statement within MATLAB function

조회 수: 1 (최근 30일)
Samer ALSAMADI
Samer ALSAMADI 2021년 7월 20일
답변: Steven Lord 2021년 7월 20일
Hello,
I am coding a logarithmic quantizer using the MATLAB function block in Simulink. The quantizer has an input u and an output , according to the following equations:
, if and
- , if and
(The set of integers).
Given: , u = positive slope ramp signal.
I tried coding the problem myself as follows, but kept receiving the error message: "Variable 'y' is not fully defined on some execution paths.", where y is the quantizer output ( ). I am able to code the above functions for j = 1 for example, but not for j = the set of integers.
My question is, how can I use a while loop within the MATLAB function such that so that I don't receive the error message above?

채택된 답변

Steven Lord
Steven Lord 2021년 7월 20일
Define y on all the execution paths. Consider this example:
q = myfun(5)
Output argument "out" (and maybe others) not assigned during call to "solution>myfun".
function out = myfun(x)
if x < 1
out = 2;
elseif x > 10
out = 3;
end
end
If x is in the interval [1, 10] then out never gets assigned a value. This is a slightly different error message than the one you're receiving but it's the same cause. In the example above I'd probably add an else statement defining the value out should have if x is neither less than 1 nor greater than 10 or I would assign out that "default" value before the if statement.
Of course, if you're operating on a vector of data the same general principle applies but you need to take into account how if behaves when given a non-scalar condition.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by