Академический Документы
Профессиональный Документы
Культура Документы
______________________________________________________________________________________________
Experiment No. 4
Input / output of Variable
Characters and Text
To enter text into MATLAB, one can use single quotes. For example,
» S = 'Hello'
The result is not the same kind of numeric matrix or array we have been dealing
with up to now. The string is actually a vector whose components are the numeric
codes for the characters (the first 127 codes in ASCII). The length of S is the
number of characters. It is a 1-by-5 character array. A quote within the string is
indicated by two quotes.
Concatenation with square brackets joins text variables together into larger strings.
For example,
» h = ['MAT','LAB']
joins the strings horizontally and produces
h =
MATLAB
and the statement
» v = ['MAT';'LAB']
joins the strings vertically and produces
(4 - 1)
University of Zakho Petroleum Engineering Department MATLAB Laboratory
______________________________________________________________________________________________
v =
MAT
LAB
Note that both words in v must have the same length. The resulting arrays are both
character arrays; h is 1-by-6 and v is 2-by-3.
Some String Function
Function Description
(4 - 2)
University of Zakho Petroleum Engineering Department MATLAB Laboratory
______________________________________________________________________________________________
Input of Variable
To enter matrix or vector or single element:
» x=input('parameter = ')
parameter = 2
x =
2
» x=input('parameter = ')
parameter= [2 4 6]
x =
2 4 6
(4 - 3)
University of Zakho Petroleum Engineering Department MATLAB Laboratory
______________________________________________________________________________________________
» x=input('parameter = ')
Parameter = [1 2 3;9 5 6]
x =
1 2 3
4 5 6
(4 - 4)
University of Zakho Petroleum Engineering Department MATLAB Laboratory
______________________________________________________________________________________________
To enter text:
» x = input('parameter = ')
parameter = 'ahmed'
x =
ahmed
» x = input('parameter= ','s')
parameter= ahmed
x =
ahmed
Output of Variable
The simplest way to display the output is the leaving of the semicolon for an
expression, or by using the disp (x) command. Where, if x is a string, the text is
displayed
» x=[1 2 3];
» x
x=
1 2 3
» disp (x)
1 2 3
Example:
» a = 60;
» b = a + 3;
» s = 'Ahmed took ';
» w = 'Mohammed took ';
» t = ' in AutoCAD';
» t = ' in Matlab';
» disp([s num2str(a) t]);
» disp([w num2str(b) t]);
(4 - 5)
University of Zakho Petroleum Engineering Department MATLAB Laboratory
______________________________________________________________________________________________
Exercises:
1- If x = [1 5 9; 2 7 4], then
a) Display the last two elements by using disp command.
b) Display the sum of each row as show below
The sum of 1st row =
The sum of 2nd row =
3- Below the lists of degrees for three students. Create M-file to calculate the
average for each student,.
Name Mathematic AutoCAD Electricity
Dilovan 77 68 80
Jin 82 74 73
Mohammed 69 89 80
Name Degree
----------------------------
Dilovan ?
Jin ?
Mohammed ?
(4 - 6)