Do I need a 'For' statement or a 'While' statement to loop back in my script?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hello Community,
I have a script that requires a bit of user input at various stages, and requires an image to be visually checked by the user before carrying on. The user has to create a viewing box in an image (ie just to look at a specific area) and then check the binary image output. Sometimes though, the viewing box is input wrong, so the X Y parameters have to be put in again. Here is the code to be cycled through when necessary:
% User input...
Y1 = input('Enter the first Y value (Top of image): \n');
Y2 = input('Enter the second Y value (Bottom of image): \n');
X1 = input('Enter the first X value (Left of image): \n');
X2 = input('Enter the second X value (Right of image): \n');
% Create binary image with the parameters of the user input
figure
i = myImage(Y1:Y2, X1:X2, 1);
bi = (i<80);
imagesc(bi);
colormap gray
axis image
So at this point, the Binary image is shown - but if its wrong, I want to cycle back to the first user input so the X Y extents can be entered again. I want a stop/checkpoint with something like this:
input('Is the binary image correct? Y/N: \n');
To make the user confirm this is OK to proceed - but I have not been able to write the loop so that it goes back to the start point that I want.
Does anyone have any ideas on how to write the necessary loop for this please?
Kind regards,
10B.
採用された回答
Something like this should work though I haven't tested it in actual code:
isvalid = false;
while( ~isvalid )
...
userStr = validatestring( input('Is the binary image correct? Y/N: \n', 's'), { 'Yes', 'No' } );
isvalid = strcmp( userStr, 'Yes' );
end
11 件のコメント
- You might prefer to use strcmpi instead of strcmp, to allow case-insensitive matching, otherwise 'yes' would be treated as a negative answer.
- You might like to consider a maximum number of iterations, or a function return if the user does not choose yes or no.
Hello Adam,
Thanks for your answer. I popped it in the code after my "axis image" line, and it works as a stop break for user input and does indeed continue when 'Yes' is input, so thanks for that.
The one part it doesn't do is cycle back to the start of the user input for the Y1 value when a negative or No etc. is returned. Do you know why this is?
Thanks.
Can you post in how you inserted it in your code. Obviously for simplicity I missed out all your code from my answer and only added the extra parts.
If your 'isvalid' boolean is not correctly returning false when the user types 'No' then it will not loop back. I may have made a mistake in my logic somewhere in that part.
The validatestring part means that it shouldn't require a strcmpi to get the correct logic, but that wouldn't do any harm certainly. What that does is to expect 'Yes' or 'No', but it is clever enough to match things like 'y', 'yes', 'no', n' and convert them to 'Yes' or 'No' as appropriate so that provided the user entered something that it can interpret as 'Yes' or 'No' those are the only two strings that will ever be fed to strcmp.
I don't know exactly how that logic is programmed though to the extent of whether it would interpret "Yeah" as 'Yes", but certainly capitalisation and contractions of the expected strings get caught fine.
Hello Adam,
I have done this:
figure
i = LensCACorr(Y1:Y2, X1:X2, 1);
bi = (i<80);
imagesc(bi);
colormap gray
axis image
isvalid = false;
while( ~isvalid )
...
userStr = validatestring( input('Is the binary image correct? y/n: \n', 's'), { 'Yes', 'No' } );
isvalid = strcmpi( userStr, 'Yes' || 'Y');
end
I have also tried to use the 'or' operator () to allow either Yes or Y to be entered as well - but this just generates an error.
Have you put a breakpoint in or just miss the ';' off the relevant lines to put the result on command line to see what the userStr and the isvalid logic are coming out as?
The 'OR' logic in strcmp definitely won't work and is unnecessary.
I tried this:
while( ~isvalid )
disp( 'Hello' )
userStr = validatestring( input('Is the binary image correct? y/n: \n', 's'), { 'Yes', 'No' } );
isvalid = strcmpi( userStr, 'Yes' );
end
which seems to do exactly what I expect, e.g.
Hello
Is the binary image correct? y/n:
n
Hello
Is the binary image correct? y/n:
no
Hello
Is the binary image correct? y/n:
No
Hello
Is the binary image correct? y/n:
yes
>>
I think I have got it working now, but perhaps this is not the right way? Basically I just put the start of your while statement in the wrong place, but I now have it right I think:
isvalid = false;
while( ~isvalid )
Y1 = input('Enter the first Y value (Top of image): \n');
Y2 = input('Enter the second Y value (Bottom of image): \n');
X1 = input('Enter the first X value (Left of image): \n');
X2 = input('Enter the second X value (Right of image): \n');
figure
i = myImage(Y1:Y2, X1:X2, 1);
bi = (i<80);
imagesc(bi);
colormap gray
axis image
userStr = validatestring( input('Is the binary image correct? y/n: \n', 's'), { 'Yes', 'No' } );
isvalid = strcmpi( userStr, 'Yes');
end
Is there a way to have Yes, yes, Y or y also be acceptable as the user input?
Adam - I think we have cross posted a couple of times here. I have it working as expected thanks. I just put the opening of the 'while' statement in the wrong place. I suffer from a long string of the usual beginner errors like that!
Just to make it more accessible for the user; is there a way to have either 'Yes', 'yes', 'Y' or 'y' to also be acceptable as the user input?
They should all be allowed using this method. Have you tried them all? I didn't include them in my example because obviously any one of them terminates the loop whereas the 'No' options I could show numerous of.
Yes I did try all of them as potential user inputs, but aside from 'Yes' and 'yes' the others didn't work.
I also copied in this line, beneath the 'Yes' version:
isvalid = strcmpi( userStr, 'Y');
but this wasn't acted upon when 'Y' or 'y' was typed. Just to see what happens, I also tried:
isvalid = strcmpi( userStr, 'Yes', 'yes', 'Y', 'y');
but this meant that the condition was never met and it kept looping indefinitely. Ive chaged it back to just 'Yes' at the end, but still 'Y' and 'y' dont work.
In the version I am testing (as shown in the code in a comment above), both 'y' and 'Y' work fine.
It isn't to do with the strcmp though. By the time the strcmp line is executed 'userStr' will always be either 'Yes' or 'No' or the code will have thrown an error.
This is because the
userStr = validatestring( input('Is the binary image correct? y/n: \n', 's'), { 'Yes', 'No' } );
line takes 'Y', 'y', 'Yes', 'yes', 'Ye', 'yE', etc etc and converts them all to 'Yes' in userStr.
If yours isn't working then I can only guess that something in your usage of the validatestring is not working.
You can easily check what is in userStr though by just temporarily removing the ':' from the end of the line so it prints it to command line.
Adam,
I'm a bit late coming back to this - I got distracted by many other problems and this got shelved for a while!
I finally got this to work by putting my script inside a function, and wrapping your code either side of the function, meaning that if the binary image comes out wrong, I can cycle back to the first user input, go through the function and proof before continuing - all as I originally wanted to do.
Thanks again!
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Language Support についてさらに検索
製品
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
