Do I need a 'For' statement or a 'While' statement to loop back in my script?

2 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Adam
Adam il 5 Ott 2015
Modificato: Adam il 5 Ott 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 Commenti
Adam
Adam il 6 Ott 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.
10B
10B il 1 Dic 2015
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!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by