Matlab_03b

Variables that are not numbers

Like all programming languages Matlab allows us to give values to variables that are not numbers

% set a string value

my_name = 'Martin'

Note: single quotes for the value of the variable

Variables that hold words or sentences are often called string variables because they consist of a string of characters. Strings can be manipulated, added to, tested, and so in in many ways. Here are some examples.

% set a string value

my_2nd_name = 'Coath'


% force a re-spelling

my_2nd_name(3) = 'u'

% make a longer string by concatenating three short strings

my_full_name = [my_name, ' ', my_2nd_name]

% pull out individual elements

my_initials = [my_full_name(1), '.' my_full_name(8), '.']

% more general version of previous example

spc_pos = strfind(my_full_name,' '); my_initials = [my_full_name(1), '.' my_full_name(spc_pos +1), '.']