Printing Exercise

This section contains an exercise that will get you to practice the stuff covered so far. Refer to the hints if you get stuck!


Exercise 1.3
Try to use the basics that we have just covered to create a program that stores your name, where you live (country) and which calculates your age, using a script. Can you also get MATLAB to output your information using the script files?

You will notice that at this stage, you can only have MATLAB tell you your information row by row. At a later stage, you will learn how to output the information using the following format: “Your name is [your name]. You are [your age] years old and you live in [your country].”

If you have problems with displaying the age, refer to the second hint below.

Tip: It would be good to make your program user friendly and interactive. This is not an exercise of assigning values to variables. This is more a problem-solving type exercise that you need to use actual coding to solve.

Hint: First open up MATLAB and set your working directory. Then create a new script and begin coding! If you’re not sure about certain commands, go to the command window and give it a go. If it works, then use it in your script, and if it doesn’t, scan your code very closely for errors (including syntax errors and typos).

Hint: MATLAB does not display integers using the disp() command, so you must convert the integer to a string.

Refer to the Introduction to Variables section to recall how to do this. If you’re still stuck, see the next hint.

Hint: Use the num2str(variableName) command to convert the integer to a string. To do this, instead of asking MATLAB to output the variable in question (age), you want MATLAB to output num2str(age).

There are much more efficient ways of outputting information, and those will be covered after you complete this exercise. For now, check your work with the file below.