Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8112265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:25:55+00:00 2026-06-06T02:25:55+00:00

How do I in MatLab catch the error that occours when the user enters

  • 0

How do I in MatLab catch the error that occours when the user enters letters and other things that aren’t numbers in the input:

width = input('Enter a width: ');

I have played around for a while with the try/catch command:

width = 0;
message = '';

% Prompting.
while strcmp(message,'Invalid.') || width < 1 || width ~= int32(width)

  try
     disp(message)
     width = input('Frame width: ');
  catch error
     message = 'Invalid.';
  end

end

But with no luck (the above doesn’t work). As shown I would like a simple message like “Frame width: ” for the user the first time he has to enter his choice. But if an error is caught I want the message for him to be “Invalid. Try again: ” fx everytime an error occours.

I have also tried the error() but I don’t know how to place that correctly. Since the error() doesn’t take the input command, where the error happends, as an argument, it must detect it in another way, which I can’t figure.

Any help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T02:26:00+00:00Added an answer on June 6, 2026 at 2:26 am
    answer = input('Frame width: ', 's');
    [width, status] = str2num(answer);
    while ~status || ~isscalar(width) || width ~= floor(width)
      answer = input('Invalid. Try again: ', 's');
      [width, status] = str2num(answer);
    end
    disp(width);
    

    (status is 0 if the conversion failed. Without the isscalar test, an input like [1 2; 3 4] would also be accepted. The last test ensures that width must be an integer.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using MATLAB, I have this code: value = input('>> Enter a value: '); and
Matlab has msgbox, listdlg, textscan, and many other functions that Octave apparently doesn't have.
I have a problem with MATLAB that i cant quite fix. It is throwing
Apparently Matlab load loads the data from a .mat file into the variable that
Since there is no finally clause to the try-catch block in MATLAB, I find
Anyone out there used MATLAB's javaObject() function with a JAR file that uses JNI?
Matlab has a nice property that scalar functions (such as sin) can work on
In MATLAB one can change the font name of things such as ylabel ,
In matlab there is a function called KnnImpute that permit to replace missing values
I have a Matlab script consisting of several cells that should all be able

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.