Introduction to MATLAB & SIMULINK: A Project Approach, Third Edition

Chapter 3: Projects

3.1 HELLO WORLD

A traditional first programming assignment is the "Hello World" exercise. Here, the goal is simply to display text. Typically, input and output are tricky in a programming language, so once we have a quick way to show text to the user, we can build it into something complex as needed. Also, it provides a level of instant gratification to the programmer.

The disp function provides a simple way of displaying text to the user.

disp('Hello World')

Here is the response from MATLAB:

>> disp('Hello World')Hello World

3.1.1 Personalized Hello World

We can personalize the greeting by adding a name. Suppose we have the greeting ("Hello") stored as a string, and another string name which, as the name implies, stores a person's name. We can concatenate the two strings by putting the two variable names inside a set of square brackets:

>> greeting = 'Hello ';>> name = 'Jane';>> disp([greeting, name])Hello Jane

Notice the space between the two words; it is actually part of the greeting string. We could have put the space before "Jane," or even inserted a space string between the two, as shown below.

>> greeting = 'Hello';>> name = 'Jane';>> disp([greeting, ' ', name])Hello Jane<span class="beginpage"> pagenum="190"><a name="424"></a><a name="IDX-190"></a></span>

Using the square brackets is one option to concatenate strings, but MATLAB provides another way to do this with the Strcat command.

>> greeting = 'Hello';>> name = 'Jane';>> disp(strcat(greeting, ' ', name))HelloJane

Why did the two words run together? The...

UNLIMITED FREE
ACCESS
TO THE WORLD'S BEST IDEAS

SUBMIT
Already a GlobalSpec user? Log in.

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.

Customize Your GlobalSpec Experience

Category: Magnetic Stripe Readers and Magnetic Stripe Encoders
Finish!
Privacy Policy

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.