Matlab Lab3
Matlab Lab3
Halmstad University
School of Information Science, Computer and Electrical Engineering
Laboratory 3
Length about 2 hours with supervision.
Regardless if you completed the whole Lab-PM during these 2 hours you should try
to find the time to work through this Lab-PM thoroughly afterward.
The last page contains hand-in assignments. You should try to solve them within a
week to be able to follow the pace in the course.
Everything that follows after >> is written in the Matlab Command Window and
indicate what you as a user should write. The % is intended as a comment to that
specific command.
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 then be
overwritten.
You can at any time find out the actual value of a variable simply by writing it 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 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 index.
Exercise 1: Assign a variable to a string of characters !
>> words=This is a string
Press enter to see the content of the variable words. Words has the size 1x16 elements.
The class is char ( character) and storage size 32 bytes.
>> words(2)
ans = h
>> words(2)=t % replaces the second element in the vector with t.
Eventually in manyprograms we need some form of interactivity. For instance to get
input from the user. Matlab takes data from the matlab prompt.
>> x= input(Give me a name !: ,s); % Matlab expects a number, string or a matrix
% from the keyboard.
% The second argument says it
% should be a string.
>> disp(The name is: ), disp(x)
% Gives text as output and the value of x.
We can also introduce the escape character sequences \n for the command input.
This can be written as:
>> x= input(Give me a name !: \n,s); % Skips a row.
Read a matrix from the command window and then display it afterwards.
Use input and disp ! It is no difference from reading a number.
>> A=input(Give me a matrix\n); disp(A)
We shall now look on another example where we have a interaction with the command
window. Now we will use a trigonometric function as a string input. The m-file looks
like below:
----------------------------------------------------------------------------------% The m-file was created 060201 by Thomas Munther
% The user can choose between three trigonometric functions and have these plotted.
% The command fplot have two input arguments. The first one is a string and the second
% one is a vector that gives the plot interval.
fcn= input(Choose one of the functions: sin, cos or tan : , s)
fplot(fcn,[0 40])
-----------------------------------------------------------------------------------We stated earlier that a string of characters is a row vector and this is nothing more than
a special case of a matrix, so we can create matrices consisting of strings, but note that
these strings must have the same lengths. Otherwise each element in the matrix will differ
and we can not perform the basic operations such as addition, subtraction and so on.
Now we present some typical swedish names as strings:
>> S1=Anders
% 8 characters, two are blank.
>> S2=Nils
% 8 characters, four are blank.
>> S3=Johanna
% 8 characters, one is blank
>> S4=Klara
% 8 characters, three are blank.
>> A=[ S1 S2 S3 S4]
% a matrix A, its elements are strings.
It is possible to assemble strings of characters in many different ways, but sometimes
you would also like to have numbers inserted in the strings. This can be arranged by
using conversions. We shall now try the command num2str. It means we make a
coversion from a numerical value to a string.
>> x=num2str(123.4567,3) % converts the number to a string with three characters.
>> u=[The number is , x, , who would have guessed !];
disp(u)
There are lots of conversions in matlab. In the table below we show a few of them:
int2str(n)
Converts an integer n to a string
hex2num(hstr)
Converts hexadecimal number hstr to a float.
hex2dec(hstr)
Converts hexadecimal string to decimal integer.
dec2hex(n)
Converts decimal integer to hexadecimal string.
dec2base(n,base)
Converts decimal integer to base B string.
bin2dec(str)
Converts binary string to decimal integer.
dec2bin(n)
Converts decimal integer to a binary string.
mat2str(A,n)
There are of course many other conversions, but we will stop here and try a few of them:
>> dec2hex(10)
>> dec2hex(12)
For those of you who have not heard about hexadecimal numbers: it simply means that
the base is 16, but the representation uses the numbers 0,1,2...9, A,B.....F instead of the
numbers 0, 1, 2.....9, 10,11,...15.
Also try to go back from a hexadecimal representation to decimal representation.
Do not forget that the argument is a string.
What becomes dec2base(4,3) ?
>> dec2base(4,3)
% converts the number 4 (base 10) to a number with base 3.
There are logical functions for strings and functions to pick substrings.
We have functions to add blanks and subtract characters. We can also alter from
small to capital letters and vice versa. We can also decide if we have letters in a string.
Comparision of strings are also possible and to decide if they are equal and many other
things.
I choose to examplify the following:
Blanks(n)
Gives a string with n blanks
deblank(str)
Subtracts all blanks at the end of the string.
Lower(str)
All letters are changed to small.
Upper(str)
All letters are changed to capital.
Ischar(str)
If string contains character => gives 1 in return, 0 otherwise.
isletter(str(i))
If element number i in the string is a letter=> gives 1 in return.
isspace(str)
True for white space characters.
strcmp(str1,str2) returns 1 if strings S1 and S2 are the same and 0 otherwise.
strcmpi(str1,str2) returns 1 if strings S1 and S2 are the same except for
case and 0 otherwise.
strfind(str1,str2) returns the starting indices of any occurrences of the string str2 in the
string str1.
findstr(str1,str2) returns the starting indices of any occurrences
of the shorter of the two strings in the longer.
Introduce a string str1=Whats your name ? % Put some blanks after the letters.
and yet another string str2= Whats your name ? % without any blanks.
Try some of the commands in the previous table.
Especially the commands deblank,lower, upper, ischar, isletter and isspace with the
argument str1.
>> isletter(str1(3))
>> isletter(str1(4))
Also try to compare strings. What wiil be result of the following:
>> strcmpi(deblank(str1),str2)
% deblank(str1), creates a new string.
and finally to examine the last to commands in the table.
>> str3=name
Try !
>> strfind(str1,str3)
>> findstr(str1,str3)
Exercise 2:
eval(str)
eval(str,alt)
Charles
Anderson
23
football
Gwen
Nolan
34
golf
Figure 1
We will also examplify how cell2struct can be used. If the concept structure is not known
from programming you will also have a possibility later on in the course to get to know
the concept. Structure is a variable containing different fields. Think of it as a method of
organizing the variable. Each field can be accessed by point notation.
>> E=cell2struct(A,{first_name,last_name,age,sport},2)
% Notice that dimension is now 2.
E=
2x1 struct array with fields:
first_name
last_name
age
sport
Display the different fields within the structure:
E(1)
ans =
first_name: 'Charles'
last_name: 'Anderson'
age: 23
sport: 'football'
E(2)
ans=
first_name: 'Gwen'
last_name: 'Nolan'
age: 34
sport: golf