Matlab Tutorial 3: Strings in Matlab

The concept of variables is fundamental in all programming. In Matlab you introduce variables and assign values to them all by yourself. When you assign a value to a variable its previously assigned value will be overwritten. You can at any time find out the actual value of a variable simply by writing the variable name in the command window (without ;).

The class is decided by the assigned value.

>> a=1; % the class is double and the storage size is 8 bytes.

Open and check workspace.

In this laboratory we will investigate strings and conversions of strings among other things. A string is stored as a row vector, so that every element represents a character. Each character is accessible by the name of the vector and its indice.

Exercise 1: Assigning a variable to a string of characters

>> words='This is a string'

Press on enter to see the content of the variable words. Words has the size 1×16 elements. The class is char (character) and storage size 32 bytes.

>> words(2) % gives the second element in the variable words.
ans = h
>> words(2)='t' % replaces the second element in the vector with t.

Sooner or later we need some form of interactivity. For instance to get input from the user. To prompt the user for an input:
Read more »