DOC PREVIEW
UCF EGN 3420 - Character Strings

This preview shows page 1-2-3-4-5 out of 15 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1Chapter 10 Character Strings 10.1 String Construction In MATLAB, text is stored as character strings, or strings for short. Strings are numerical arrays of ascii values which are displayed as the equivalent characters. Strings are created by enclosing text within single quotes. Example 10.1.1 clear s=' Hello there.' x=size(s) greeting='Welcome to the University of Central Florida!' y=size(greeting) whos s = Hello there. x = 1 13 greeting = Welcome to the University of Central Florida! y = 1 45 Name Size Bytes Class greeting 1x45 90 char array s 1x13 26 char array x 1x2 16 double array y 1x2 16 double array Grand total is 62 elements using 148 bytes The elements of a string array are the individual characters, each of which requires 2 bytes of storage compared to 8 bytes for elements in numerical, i.e. double arrays. The ascii representation of a string is obtained by making it the argument of the function 'double'. The functon 'char' restores the character string. Example 10.1.2 s1='text are character strings' size_s1=size(s1) z=double(s1) % Convert string to ascii representation size_z=size(z) s2=char(z) % Convert numerical array of ascii values back to string s3=char(100:107) % Convert ascii values 100-107 to characters s1 = text are character strings2size_s1 = 1 26 z = Columns 1 through 12 116 101 120 116 32 97 114 101 32 99 104 97 Columns 13 through 24 114 97 99 116 101 114 32 115 116 114 105 110 Columns 25 through 26 103 115 size_z = 1 26 s2 = text are character strings s3 = defghijk Strings can be addressed and manipulated the same as arrays. Strings are also concatenated the same as arrays. Example 10.1.3 s1='Greetings sport fans.' word2=s1(11:15) s2='baseball' s3=['Greetings ' s2 ' fans.'] s4=s3' s1 = Greetings sport fans. word2 = sport s2 = baseball s3 = Greetings baseball fans. s4 = G r e e t i n g s b a s e3b a l l f a n s . To display a string without printing its name, the function 'disp( )' with the string variable name as argument is used. Example 10.1.4 s1='Joe said ''Have a nice day'' ' disp(' ') disp(s1) disp(' ') disp('Happy Birthday') s1 = Joe said 'Have a nice day' Joe said 'Have a nice day' Happy Birthday Character strings arrays can have more than one row provided each row is a string with the same number of elements (characters). Example 10.1.5 list=['Harold Klee';'Chris Bauer';'Joe Smith ']; % Two blanks are added to the end of Joe Smith to make it 1 ×× 11, % the same size as strings Harold Klee and Chris Bauer disp(list) size_list=size(list) Harold Klee Chris Bauer Joe Smith size_list = 3 11 The MATLAB function 'char( )' is also used to create multiple row string arrays from individual strings of different lengths. The function 'strcat( )' is used to horizontally concatenate string arrays with the same number of rows.4Example 10.1.6 NLteams=char('N Y Mets','Atlanta Braves','Milwaukee Brewers'); disp(NLteams),disp(' ') size_NLteams=size(NLteams),disp(' ') ALteams=char(' Chicago White Sox',' Seattle Mariners',' Texas Rangers'); disp(ALteams),disp(' ') size_ALteams=size(ALteams),disp(' ') teams=strcat(NLteams,ALteams); disp(teams),disp(' ') size_teams=size(teams),disp(' ') N Y Mets Atlanta Braves Milwaukee Brewers size_NLteams = 3 17 Chicago White Sox Seattle Mariners Texas Rangers size_ALteams = 3 18 N Y Mets Chicago White Sox Atlanta Braves Seattle Mariners Milwaukee Brewers Texas Rangers size_teams = 3 31 When the function 'char( )' is used to create string arrays with multiple rows, individual rows may contain padded zeros to assure that all rows are the same length. The padded zeros can be removed after accessing the individual rows (1 × n string arrays) by 'deblank'ing them. Example 10.1. 7 schools=char('Central Florida','Harvard','Florida State'); disp(schools),disp(' ') FLschools=[schools(1,:);schools(3,:)]; disp(FLschools), disp(' ') other1=schools(2,:) size_other1=size(other1),disp(' ') other2=deblank(schools(2,:)) size_other2=size(other2) Central Florida Harvard Florida State Central Florida Florida State other1 = Harvard5size_other1 = 1 15 other2 = Harvard size_other2 = 1 7610.2 Numbers to Strings to Numbers Often times it is desirable to convert information from one data type to another. In particular, it is common to transform numerical data into text strings for establishing the flow of executable commands within MATLAB's programming environment. In addition, numbers are sometimes converted to strings for annotation of charts and graphs. The essential MATLAB functions for accomplishing data type conversions involving integer and floating point numbers are 'int2str', 'num2str', and 'sprintf'. Example 10.2.1 x=[1:3,5:7,9:11] % x is an integer array disp(class(x)) size_x=size(x),disp(' ') xs=int2str(x) % xs is a character array disp(class(xs)) size_xs=size(xs),disp(' ') xs10=xs(1:10) % xs10 is a string array of the first 10 characters of xs x = 1 2 3 5 6 7 9 10 11 double size_x = 1 9 xs = 1 2 3 5 6 7 9 10 11 char size_xs = 1 33 xs10 = 1 2 3 A=25+5*randn(2,3) % A is a floating point array disp(class(A)) size_A=size(A), disp(' ') As=num2str(A) % As is a character array disp(class(As)) size_As=size(As) As2=As(2,10:end) % As2 is a portion of the second row of As A = 22.8372 25.6267 19.2676 16.6721 26.4384 30.9546 double size_A = 2 3 As = 22.8372 25.6267 19.2676 16.6721 26.4384 30.95467char size_As = 2 33 As2 = 26.4384 30.9546 The function 'sprintf' is similar to the ANSI C function and is used for creating character strings which can be displayed or manipulated. Very often the string is comprised of text and numerical data stored in variables which is converted to character strings according to a specified format conversion. Example 10.2.2 format long p=pi*1000, disp(' ') p_s1=sprintf('%.5e\n',p) % Exponential conversion and display 5 % places after the decimal point p_s2=sprintf('%.5f\n',p) % Fixed point conversion and display 5 % places after decimal


View Full Document

UCF EGN 3420 - Character Strings

Download Character Strings
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Character Strings and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Character Strings 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?