0% found this document useful (0 votes)
3 views

Experiment No 5

The document summarizes a lab session on loop statements, string functions, cell arrays, combining strings, and plotting in MATLAB. It includes examples of calculating a factorial using a while loop, converting strings to ASCII and back, creating and manipulating cell arrays of strings, concatenating strings, and plotting a function and its derivatives.
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)
3 views

Experiment No 5

The document summarizes a lab session on loop statements, string functions, cell arrays, combining strings, and plotting in MATLAB. It includes examples of calculating a factorial using a while loop, converting strings to ASCII and back, creating and manipulating cell arrays of strings, concatenating strings, and plotting a function and its derivatives.
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/ 4

LAB Session No.

5
Loop statements
%calculating factorial
1_x=input('type number:');
fact=1; while 1_x>1
fact=fact*1_x;
1_x=1_x-1; end disp(fact)

type number:5
120

String functions, cell arrays and combining strings

my_string = '1''s Point';


str_ascii = uint8(my_string)
str_back_to_char= char(str_ascii)
str_16bit = uint16(my_string)
str_back_to_char =
char(str_16bit)

str_ascii = 1×14 uint8


row vector
Columns 1 through 13
72 65 83 83 65 78 39 115 32 80 111 105 110
Column 14
116
str_back_to_char =
'1's Point' str_16bit =
1×14 uint16 row vector
Columns 1 through 13
72 65 83 83 65 78 39 115 32 80 111 105 110
Column 14
116
str_back_to_char =
'1's Point'

Cell Arrays

doc_profile = ['1 '; ...


'Sr. Surgeon '; ...
'R N Tagore Cardiology Research Center']
doc_profile =
3×37 char array
'1 '
'Sr. Surgeon '
'R N Tagore Cardiology Research Center'
doc_profile = char('1', 'Sr. Surgeon', ...
'RN Tagore Cardiology Research Center')

doc_profile =
3×36 char array
'1 '
'Sr. Surgeon '
'RN Tagore Cardiology Research Center'

Combining strings
name = '1 ';
position = 'Sr. Surgeon
';
worksAt = 'R N Tagore Cardiology Research
Center'; profile = [name ', ' position ', ' worksAt]
profile = strcat(name, ', ', position, ', ', worksAt)

profile =
'1 , Sr. Surgeon , R N Tagore Cardiology Research Center'
profile =
'1,Sr. Surgeon,R N Tagore Cardiology Research Center'

Combining strings into arrays

name = '1 ';


position = 'Sr. Surgeon
';
worksAt = 'R N Tagore Cardiology Research
Center'; profile = char(name, position, worksAt);
profile = cellstr(profile); disp(profile)

{'1' }
{'Sr. Surgeon' }
{'R N Tagore Cardiology Research Center'}

String functions

str_array = {'red','blue','green', 'yellow',


'orange'}; str1 = strjoin(str_array, "-") str2 =
strjoin(str_array, ",")
str1 =
'red-blue-green-yellow-orange'
str2 =
'red,blue,green,yellow,orange'

PLOTTING

1_x=[-2:0.2:4];
1_y=2*1_x.^3 - 7*1_x.^2 + 5*1_x.^1;
1_yd=6*1_x.^2 - 14*1_x + 5; 1_ydd=12*1_x -
14;
plot (1_x,1_y,'-
mo','LineWidth',2,'markersize',8,'markerfacecol
or','y') hold on xlabel('TIME (s)')
ylabel('FUNCTION')
title('\fontname{hello}Distance Velocity and
Acceleration as a function of
Time','Fontsize',15) axis([-2.5,4.5,-65,65])
gtext('Function 2x^3-7x^2+5x and its
derivarives','EdgeColor','k','LineWidth',2,'Fontsi
ze',15) plot (1_x,1_yd,'--
bv','LineWidth',2,'markersize',8,'MarkerEdgeCo
lor','b','markerfacecolor','r') hold on
plot(1_x,1_ydd,':ko','LineWidth',2,'markersize',
8,'MarkerEdgeColor','g','markerfacecolor','m')
legend('Distance','Velocity','Acceleration','Locat
ion','southeast','Fontsize',15) grid on hold off

You might also like