MATLAB Answers

New to matlab, help?

5 views (last 30 days)
Hi, our class has startet learning matlab, but we just got some mandatory tasks and i barely know anything about the program yet. Although i do have almost a month to do it i really need helping hand in where to start. I'll copy in one of the tasks below:
Create a program that asks the user to enter a number using digits and stores the number in a variable. The program should respond "Thank you!" And quit if users enter a number. If the user enters something that is not a number, the program should notify the user and ask again. The program should continue to ask again until the user enters a number. It is not necessary for the program to recognize numbers written in letters. For example, the program must understand that the entry 50 is a number, but does not need to understand that the entry "fifty" is also a number.
A little help to get started would be wonderfull!
Hints for the task was tr2num and isempty.

  1 Comment

Steven Lord
Steven Lord on 30 Oct 2019
Since you said you "barely know anything about the program yet", my first recommendation is to work through the free, roughly two hour long, MATLAB Onramp tutorial. The goal of this tutorial is to teach you the basics of working with MATLAB.
As for the hint you received, I'd be wary of using str2num. Instead I recommend str2double. str2num evaluates the text you pass into it, which would mean if your user entered the text 'exit' at your prompting MATLAB would exit because the exit function was called. str2double doesn't do that. You would have to use a slightly different function to detect the user entering something that's not a number, so I'll give an alternate hint: isnan instead of isempty.

Sign in to comment.

Accepted Answer

Thomas Satterly
Thomas Satterly on 30 Oct 2019
Since this is an assignment, I'm not going to just give you the code, but there are a few pages you should look at to get you on the right track.
First of all, I think you meant "str2num" instead of "tr2num". However, I'd recommend using str2double instead - it's usually preferred and less arbitrary.
Also, check out the input command. That's the easiest way to prompt for user data, but there's certiantly flashier options if you want a pop-up window or more robust input handling.

  0 Comments

Sign in to comment.

More Answers (0)

Sign in to answer this question.