From: Walter Roberson on
smith Og wrote:

> try /catch works.
>
> However, to go a step further, when I use the try and catch , is there a
> way to invoke the try again.
>
> For example,
> try
> statements
> statements
>
> catch
> errordlg('error in your input')
> end
>
> Is there a way I can return the command back to the begining of the try
> statement, so that if the user now puts a valid input , then no error is
> generated.

needanswer = true;
while needanswer
try
%statement
needanswer = false;
catch
errordlg(...)
continue
end
end
From: us on
"Andy "
> % something like this:
>
> validInput = false;
> userInput = input('Get user input here: ');
>
> while ~validInput
> try
> process(userInput); % do whatever to the input
> validInput = true;
> catch
> disp('Error in your input')
> validInput = false;
> end
> end

note: unfortunately, this may end in an endless loop...

us
From: Andy on
"us " <us(a)neurol.unizh.ch> wrote in message <i36tf1$2b0$1(a)fred.mathworks.com>...
> "Andy "
> > % something like this:
> >
> > validInput = false;
> > userInput = input('Get user input here: ');
> >
> > while ~validInput
> > try
> > process(userInput); % do whatever to the input
> > validInput = true;
> > catch
> > disp('Error in your input')
> > validInput = false;
> > end
> > end
>
> note: unfortunately, this may end in an endless loop...
>
> us

Apologies, us is of course correct. The request for input should be inside the loop (so the user can enter new input on each iteration), rather than before the loop.
From: smith Og on
"Andy " <myfakeemailaddress(a)gmail.com> wrote in message <i36tvr$6no$1(a)fred.mathworks.com>...
> "us " <us(a)neurol.unizh.ch> wrote in message <i36tf1$2b0$1(a)fred.mathworks.com>...
> > "Andy "
> > > % something like this:
> > >
> > > validInput = false;
> > > userInput = input('Get user input here: ');
> > >
> > > while ~validInput
> > > try
> > > process(userInput); % do whatever to the input
> > > validInput = true;
> > > catch
> > > disp('Error in your input')
> > > validInput = false;
> > > end
> > > end
> >
> > note: unfortunately, this may end in an endless loop...
> >
> > us
>
> Apologies, us is of course correct. The request for input should be inside the loop (so the user can enter new input on each iteration), rather than before the loop.

Thanks, Andy, Walter, Us.

Exactly what I needed