KEMBAR78
Experiment No. 4 Input / Output of Variable: Characters and Text | PDF | Matlab | String (Computer Science)
0% found this document useful (0 votes)
138 views6 pages

Experiment No. 4 Input / Output of Variable: Characters and Text

The document discusses input and output of variables in MATLAB. It explains that strings can be entered using single quotes and concatenated using strcat. Numbers can be input using input and converted to strings with num2str. Matrices can be input row by row. Output can be displayed by removing the semicolon or using disp, which displays strings. Exercises at the end demonstrate inputting matrices and calculating averages.

Uploaded by

kurddoski28
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)
138 views6 pages

Experiment No. 4 Input / Output of Variable: Characters and Text

The document discusses input and output of variables in MATLAB. It explains that strings can be entered using single quotes and concatenated using strcat. Numbers can be input using input and converted to strings with num2str. Matrices can be input row by row. Output can be displayed by removing the semicolon or using disp, which displays strings. Exercises at the end demonstrate inputting matrices and calculating averages.

Uploaded by

kurddoski28
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/ 6

University of Zakho Petroleum Engineering Department MATLAB Laboratory

______________________________________________________________________________________________

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

char (x) Converts the array x that contains positive


integers representing character codes into a
» char(100) MATLAB character array (the first 127
ans = codes in ASCII).
d
» char([72 69 76 76 79])
ans =
HELLO

double (s) converts the character array to a numeric


matrix containing floating point
» double('z') representations of the ASCII codes for each
ans = character.
122
» double('good')
ans =
103 111 111 100

strcat(S1,S2,...) joins S1, S2, ... variables horizontally


together into larger string.
» strcat('Hello','Ahmed')
ans =
Hello Ahmed

(4 - 2)
University of Zakho Petroleum Engineering Department MATLAB Laboratory
______________________________________________________________________________________________

strvcat(S1,S2,...) joins S1, S2, ... variables vertically together


into larger string.
» strvcat('good','bye')
ans =
good
bye

S = num2str (x) converts the variable x into a string


representation s.
» num2str(20)
ans =
20 % as a string,
not a number

S = str2num (x) converts character array representation of a


matrix of numbers to a numeric matrix.
» str2num('20')
ans =
20
Note
The printable characters in the basic ASCII character set are represented by the
integers 32:127. (The integers less than 32 represent nonprintable control
characters). Try
» char(33:127)

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]);

The execution result is:


Ahmed took 60 in AutoCAD
Mohammed took 63 in Matlab

(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 =

2- Write a program in M-File to read 3 × 3 Matrix, then display the diagonal of


matrix as shown below:
The Diagonal of This Matrix = [ ]

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

Display the result as shown below

Name Degree
----------------------------
Dilovan ?
Jin ?
Mohammed ?

(4 - 6)

You might also like