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

Chapter 7

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Chapter 7

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Chapter 7: MATLAB Programs

Contents:
1. Creating String Variables
2. Operations On Strings
3. The "Is" Functions For Strings
4. Converting Between String And Number Types
1. CREATING STRING VARIABLES7
Example:
1. Finding the Length of a String:
1.1. Strings as Vectors
str = 'Hello, MATLAB!’;
Strings are treated as vectors
len = length(str);
2. Accessing Individual Characters or a subset of a
string :
char1 = str(1);
char2 = str(5);
char3 = str(3:5);
1. CREATING STRING VARIABLES
Example:
1.1. Strings as Vectors 1. Creating a Character Matrix:
A character matrix can be created charMatrix = ['Hello '; 'MATLAB'; 'World ']
where each row consists of a string. % Every row must have the same number of
characters
2. Check the size of the matrix:
size(charMatrix)
3. Accessing the elements:
charMatrix(1,:);
charMatrix(2,:);
charMatrix(:,2); char13 = charMatrix(1, 3),…
2. OPERATIONS ON STRINGS
Example:
2.1. Concatenation 1. Concatenation of Strings Using Square
Brackets:
Concatenation is the process of
joining arrays together. For str1 = 'Hello, ‘;
strings or character arrays, str2 = 'World!’;
concatenation allows to concatenatedStr = [str1 str2];
combine multiple strings into 2. Concatenation of Strings Using strcat
one. MATLAB provides different function:
ways to concatenate strings, concatenatedStr = strcat(str1, str2);
including using square brackets
3. Concatenating Strings in a Matrix:
[], the strcat function.
concatenatedStr = strcat(str1; str2);
2. OPERATIONS ON STRINGS
2.2. Creating Customized Strings

Note: The strcat function will remove trailing blanks


(but not leading blanks) from strings before
concatenating.
2. OPERATIONS ON STRINGS
Example:
2.2. Creating Customized 1. Using sprintf for Customized Strings:
Strings name = 'John';
In MATLAB, you can create age = 25;
customized strings by using customStr = sprintf('Hello, my name is %s and
functions: sprintf, blank. I am %d years old.', name, age);
2. Combining Text and Variables in a Loop:
for i = 1:5
customStr = sprintf('This is iteration
number %d.', i);
disp(customStr);
end
2. OPERATIONS ON STRINGS
Example:
2.2. Creating Customized Strings 1. Create a String of Blanks
The blank function creates a string spaces = blanks(5); % Creates a string
of spaces (blanks) with a specified with 5 blank spaces
length. This can be useful when 2. Combining Blank Spaces with Other
you need to insert a certain Strings:
number of spaces in a string or result = ['MATLAB' blanks(3)
create a space-filled string for 'Programming'];
formatting purposes.
2. OPERATIONS ON STRINGS
Example: Create Prompts
2.3. Applications of Customized % Prompt the user to enter a name
Strings: Prompts, Labels, and
name = input('Enter your name: ', 's');
Arguments to Functions
2.3.1 Prompts % Prompt the user to enter an age
Customized strings can be used to age = input('Enter your age: ');
create dynamic and informative % Create a customized string using
prompts for users. This is especially sprintf
useful in scripts and functions that customPrompt = sprintf('Hello %s, you
require user input. are %d years old.', name, age);
% Display the custom prompt
disp(customPrompt);
2. OPERATIONS ON STRINGS
2.3. Applications of Customized Example: Customizing Plot Labels
Strings: Prompts, Labels, and
Arguments to Functions
2.3.2. Labels
Customized strings are often used to
generate labels for plots, graphs, and
figures in MATLAB. You can create
titles, axis labels, and legends using
dynamic content, which makes
visualizations more descriptive.
2. OPERATIONS ON STRINGS
2.3. Applications of Customized Example: Customizing Plot Labels
Strings: Prompts, Labels, and x = 0:0.1:10;
Arguments to Functions y = sin(x);
2.3.2. Labels plot(x, y);
Customized strings are often used to % Customize the labels with a variable
generate labels for plots, graphs, and frequency = 1;
figures in MATLAB. You can create xlabel('Time (seconds)’);
titles, axis labels, and legends using % Dynamic label
dynamic content, which makes
ylabel(sprintf('Amplitude (frequency =
visualizations more descriptive. %d Hz)', frequency));
title('Sine Wave');
2. OPERATIONS ON STRINGS
Example: Write a code to save
2.3. Applications of Customized Strings: data into the file with the name as:
Prompts, Labels, and Arguments to experiment_trial_n.mat, where n is an
Functions arguments.
2.3.3. Arguments to Functions: baseFileName = 'experiment';
Customized strings can be used as trialNumber = 5;
dynamic arguments when calling % Create a customized file name string
functions. This is useful in cases where fileName = sprintf('%s_trial_%d.mat',
the function needs to be flexible, baseFileName, trialNumber);
allowing inputs such as file names, plot
titles, or output formats to be generated data = rand(10); % Random data
based on certain conditions or variables. save(fileName, 'data'); % Save the data
with a customized file name
disp(['Data saved to file: ', fileName]);
3. THE "IS" FUNCTIONS FOR STRINGS
There are several "is" functions for
strings, which return logical true or false. Example: Check:
1/ Isletter: returns logical true for every 1. isletter('EK127’)
character in a string if the character is a 2. isspaee ('a b’)
letter of the alphabet or false if not.
3. isstrprop ( 'AB123. 4 ', 'alphanum‘)
2/ Isspace: returns logical true for every
character that is a white space character. 4.
3/ Isstrprop: determines whether the vec= 'EK127’;
characters in a string are in a category ischar(vec)
specified by a second argument. vec= 1:5;
4/ ischar: returns logical true if the Ischar(vec)
vector argument is a character vector (in
other words, a string), or logical false if
not.
4. CONVERTING BETWEEN STRING AND
NUMBER TYPES
Example 1:
1. Converting a String to a Number:
>>numStr = '123.45’;
MATLAB provides functions: str2num
and str2double to convert strings >>number = str2double(numStr);
containing numeric data into number >>mystr= '66 2 111 ‘;
types.
>>numvec = str2num (mystr)
2. Converting a Number to a String
MATLAB provides functions: num2str Example 2:
and int2str to convert numbers to
strings. >> number = 123.45;
str = num2str(number);
>>num= 38;
str= int2str(num)
• Bài tập về nhà:
Đọc lý thuyết và làm 10 bài tập trong chương 7 của giáo trình chính.
Stormy Attaway, Matlab: A Practical Introduction to Programming and
Problem Solving, 2nd Edition, Elsevier, 2017

You might also like