user1501127
user1501127

Reputation: 865

Loop input until correct format is given in Matlab script?

I cant find how to write the code so my Matlab script re-ask the question since the user input was in wrong format.

My code is very simple and it all works but it just skips if the user gives wrong format for a second time. Is it possible in Matlab script to have the question repeated since a if fails and the input does not pass what is asked for?

  A1 = input('State the vector: ');
    if length(A1) < 3 || length(A1) > 3
    disp('The input needs 3 values.')
    A1 = input('State the vector again please: ');
  end

How do i make it ask the question untill it passes the length of index 3?

Upvotes: 0

Views: 1872

Answers (1)

Milo
Milo

Reputation: 2171

Try this:

A1 = input('State the vector: ');
while(1)
    if length(A1) ~= 3
        disp('The input needs 3 values.');
        A1 = input('State the vector again please: ');
    else
        break;
    end
end

Upvotes: 2

Related Questions