Do I need a 'For' statement or a 'While' statement to loop back in my script?
1 view (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Adam
on 5 Oct 2015
Edited: Adam
on 5 Oct 2015
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 Comments
Adam
on 6 Oct 2015
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.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!