필터 지우기
필터 지우기

how to define a while statement that when the variable equals a numerical value, ends the code?

조회 수: 1 (최근 30일)
Hi,
I want to write a while condition that when variable A gets a numerical value inside the loop, ends the code, otherwise continues to the code...
How can I write this while condition?
Thansks in advance,
Regards...

채택된 답변

Walter Roberson
Walter Roberson 2016년 1월 19일
while ~exist('A', 'var') && ~isnumeric(A)
...
end
This will continue to loop until A is assigned a value and the value is numeric.
The common kinds of values that are not numeric include logical values, character strings, structures, cell arrays, function handles, graphics objects (R2014b or later), and various other kinds of objects.
  댓글 수: 3
Walter Roberson
Walter Roberson 2016년 1월 19일
while ~exist('A', 'var') || ~isnumeric(A) || isempty(A)
...
end
You would not need to initialize A at all
[] is type double and so is considered a numeric value. However I have added that as a condition.

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

추가 답변 (1개)

Titus Edelhofer
Titus Edelhofer 2016년 1월 19일
Hi,
I understand the "get's value" is meant as "being not empty" ...?
A = [];
while isempty(A)
% do something, e.g.
A = 42;
end
Titus
  댓글 수: 2
Titus Edelhofer
Titus Edelhofer 2016년 1월 19일
Or, if you want to run while it's not a specified value:
A = 10;
while A~=1
% do something, e.g.
A = A - 1;
end
Titus
Walter Roberson
Walter Roberson 2016년 1월 19일
And be careful, if your "specified value" is not an integer then it might be difficult to match it exactly due to floating point precision issues.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by