フィルターのクリア

While loop inside an if loop

7 ビュー (過去 30 日間)
mcm
mcm 2016 年 10 月 23 日
編集済み: Walter Roberson 2016 年 10 月 23 日
I have UPDRS1997 which is an array (1x50). I want a while loop that will display Participant should consult neurologist if the value of the array is not 0
pick_year = input('Pick a year: either 1997 or 2013: ')
if pick_year == 1997
load('UPDRS1997')
while UPDRS1997 ~=0
fprint('Participant should consult neurologist'/n)
end
end
The while loop I created however does not run. Matlab stops by display pick_year.
How can I improve my code?
  2 件のコメント
Robert
Robert 2016 年 10 月 23 日
What is 'UPDRS1997'
And where is it located? You have to give us all the info for help
Walter Roberson
Walter Roberson 2016 年 10 月 23 日
It is a .mat file in the current directory that contains the variable UPDRS1997 which is a 1 x 50 double.

サインインしてコメントする。

回答 (2 件)

Chaya N
Chaya N 2016 年 10 月 23 日
編集済み: Chaya N 2016 年 10 月 23 日
"Matlab stops by display pick_year."
When this line is displayed, you have to enter the year on your command window. Your program will only run once you specify the input!
Also, if you do not have any data associated with the year 2013, why is it specified as an input option?
  3 件のコメント
Walter Roberson
Walter Roberson 2016 年 10 月 23 日
while VARIABLE ~= 0
means the same thing to MATLAB as
while all(VARIABLE(:) ~= 0)
which is to say that it will only be true if every value in the given VARIABLE is non-zero. If you have even one 0 in there then the condition will be false at the beginning and the while will not be executed.
If the while were executed then because it the condition does not change and nothing in the body of the while changes any variable, the while would run infinitely.
Chaya N
Chaya N 2016 年 10 月 23 日
For the issue with the while loop, refer to Walter Roberson's excellent solution below.
My question was, when you are prompted for an input, if you specified 2013 as the year, your program would still terminate because there are no statements specified to execute for that condition. If you have no data for that particular input, then it is unnecessary to prompt for the year because you could directly load the data for the year 1997 and run the program. This would also eliminate the first 'if' statement for pick_year.

サインインしてコメントする。


Walter Roberson
Walter Roberson 2016 年 10 月 23 日
編集済み: Walter Roberson 2016 年 10 月 23 日
pick_year = input('Pick a year: either 1997 or 2013: ')
if pick_year == 1997
load('UPDRS1997')
if all(UPDRS1997 ~=0)
fprintf('Participant should consult neurologist, array is all non-zero\n')
elseif any(UPDRS1997 ~=0)
fprintf('Participant should consult neurologist, something in array is non-zero\n')
end
end
  2 件のコメント
Chaya N
Chaya N 2016 年 10 月 23 日
Perhaps the 'fprint' above should be 'fprintf' (?)
Walter Roberson
Walter Roberson 2016 年 10 月 23 日
Ah yes, you are right.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeText Data Preparation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by