0% found this document useful (0 votes)
91 views

Matlab Activity 2.1

The document discusses variables and strings in MATLAB. It defines variables as named storage locations that can hold values. Variable names can use letters and numbers but must start with a letter. Strings are arrays that hold text data and come in two types: character arrays denoted with single quotes and string arrays denoted with double quotes. The document provides examples of assigning variables and strings in MATLAB and using functions like ischar, isstring, strcmp to analyze string properties and compare strings.

Uploaded by

roseleen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views

Matlab Activity 2.1

The document discusses variables and strings in MATLAB. It defines variables as named storage locations that can hold values. Variable names can use letters and numbers but must start with a letter. Strings are arrays that hold text data and come in two types: character arrays denoted with single quotes and string arrays denoted with double quotes. The document provides examples of assigning variables and strings in MATLAB and using functions like ischar, isstring, strcmp to analyze string properties and compare strings.

Uploaded by

roseleen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

MATH 012/012B NUMERICAL METHODS AND ANALYSIS

LABORATORY ACTIVITY 2.1

VARIABLES AND STRINGS

NAME :
SCORE:

SECTION: ACTIVITY NO:

INSTRUCTOR: DATE PERFORMED/SUBMITTED

Variables
A variable is used to store a certain value. It has a name, such as x or myname, and this name is assigned a value like
5 or ‘Manila’.
Rules in Assigning Variables
1. Matlab variable names are case-sensitive i.e. abc, ABC and AbC are different variable names.
2. Variable names can use letters, numbers or a combination.
3. The underscore special character can be used in variable names.
4. Variable names should always start with a letter.

How long can a variable name be? Type in the command window
>> namelengthmax
Output: ________________________

What does this mean?

______________________________________________________________________________________________________________________________
Enter the following commands in MATLAB
>> a=5
>> A=15
>> a+3
>> A+5
>> A+a
>> win_win = 3
>> 2win = 3
>> win-win = 3
The last two commands return an error message. Complete the table below.

Invalid variable name MAtlab Output Reason

2win

win-win

Remarks: In assigning, it is helpful to use descriptive variable names in order for your program later on to be
understood by other people that will view your codes. For instance, if you are working on the simulation for a Mars
lander and wanted to know how far from the surface is the lander, the variable name can be altitude=3500 but the
problem is we do not know what unit is this. So the more appropriate name should be AltitudeInMeters=3500 or
altitude_in_meters=3500.

Storing Numeric Values

The vairables used in MATLAB has its own class or type which corresponds to what kind of data it holds. It can be
numbers, strings, structures etc.

Recall: In mathematics, whole numbers are called integers i.e. 3, 165, 889; while floating-point numbers are called
real numbers that contain decimal values i.e. 3.1416, 6.255, 4.7.
Type the following in the command window:
>> x = 666
>> class(x)

Note: even if we input an integer in the above command, MATLAB generated a variable of type double which is the
default type for a numeric data stored in MATLAB. Now type the following,

>>> x = int8(666);
>> class(x)

What is the difference between the output of the first and second commands?

____________________________________________________________________________________________________________________________________
___________________________________________________________________________________________________________________________________.

What is the main function of the class command?

_________________________________________________________________________________________________________________________________.
The int8 function converts the number on its argument (the value in the parenthesis) to an 8-bit integer.

For instance the number of bits is how many binary digits the value stores. For example, 8 bits can store a binary
number such as 11011011. If the sign (positive or negative) takes up one binary digit, then the value that can be
stored is reduced.

There are several integer types for different levels of size and precision. Each comes in a signed and unsigned
version. If you’re only dealing with positive integers, using an unsigned type allows you to specify numbers twice
as large.

Floating-point types can represent numbers so big that they make an int64 look like something you could count on
your fingers. This is possible because part of their stored value is an exponent. They come in two varieties: single
and double.

Constants
A constant is a value that, once assigned, cannot be changed later. MATLAB doesn’t have a straightforward way for
you to declare your own constants, but it does provide a handful of its own. There are two special floating-point
values to be aware of. You’ll see these values as the result of an operation. Type the following statements into the
Command Window and watch the result:
>> 3/0
>> inf-inf

In many programming languages, an error results if you attempt to divide by zero, but not MATLAB. It returns the
constant value Inf for infinity, or -Inf for negative infinity. Inf can be the result of any operation that results
in enormously large or small numbers. If you’ve tried to perform an operation that is not mathematically defined,
you’ll see the constant value NaN. This means “not a number.” This isn’t quite a constant by any standard definition,
but it isn’t quite anything else either. In some matrix cases it’s used as a placeholder. We’ll show you how to make
use of these values when we discuss if statements in a later tutorial. The other constant of note is pi.

Try this in the Command Window:


>> format long
>> pi
>> pi + 7
Output: _________________________________________

How many decimal places does the format long outputs?

______________________________________________________________________________________________________________________________.

Note: Inf and NaN are not case sensitive. Inf or inf, as you please. Imaginary numbers also have a special
constant, i, used to represent the imaginary portion of the value.

Strings

A string type holds text. An example of a string is this very sentence. A string can hold any valid character.
“123456” can be a string. “< <= > >=” is another. To discuss strings, we need to talk about arrays. We’ll go into
arrays and matrices in more depth in a later tutorial, so here we’ll just cover the concepts you need right now.

Array Basics
An array is a collection of data, all of the same type. Each item in the array has an index by which it can be accessed.
Consider the phrase “Carpe diem.” Each letter, space, and punctuation mark in that phrase is a character. If you
store that string as an array of characters, this is how it is stored:
Type the following lines into the Command Window and complete the following.

COMMAND WINDOW INPUT MATLAB OUTPUT


>> st = ‘Carpe diem.’

>> size(st)

>> length(st)

>> st(4)

>> st(4) = ‘9’

>> st(4) = 33

The Two String Types

There are two types of strings: a character array and a string array. Strings as character arrays are entered by
surrounding the text in single quotes ('the'). Strings as string arrays are entered by surrounding the text in double
quotes ("the"), resulting in an array with one element.

Enter the following lines in the Command Window and complete the table to explore some differences between
character arrays and string arrays:

COMMAND WINDOW INPUT MATLAB OUTPUT


*if no output, please indicate what the command does.
>> clc
>> sc = ‘This is a character array’
;
>> st = “This is a string array” ;
>> class(sc)
>> class(st)
>> size(sc)
>> length(sc)
>> size(st)
>> length(st)
>> sc(1)
>> st(1)
>> sc(2) = ‘H’;
>> sc
>> st(2) = “And this is the second
string in the array”;
>> st
Based on the outputs, what is the difference between a string as a character array and a string as a string array?

____________________________________________________________________________________________________________________________________
____________________________________________________________________________________________________________________________________
___________________________________________________________________________________________________________________________________.
String and Character Functions

MATLAB provides many functions to use with strings and characters. Here’s a list of some of the most useful. For a
complete list and usage particulars, search for “Characters and Strings” in the MATLAB help.

COMMAND WINDOW INPUT MATLAB OUTPUT


>> charArray = ‘This is a character
array.’

>> stringArray = “This is a string


array.”

>> ischar(charArray)

>> ischar(stringArray)

>> isstring(stringArray)

>> isletter(charArray)

>> isspace(charArray)

>> isletter(charArray)

>> stringArray = upper(stringArray)

NOTE: Just entering the function does not store the value for later use. If you
want to keep the results, use the equals sign and store the result in a variable
(which you can think of as a named storage location)
>> text1 = ‘Four score and seven years
ago’

>> text2 = ‘87 years ago’

>> text3 = ‘Four score and seven years


ago’

>> strcmp(text1, text2)

>> strcmp(text1, text3)

>> text1 == text3

The relational equality operator (==) looks at the string as an array, compares the individual characters, and
returns an array of logical results for each index. The strcmp function answers the question you’re really asking –

Do these two variables contain the same text?

You might also like