How to remove spaces in a string in MATLAB?


MATLAB provides various methods and functions to remove spaces in a string. In this tutorial, we will cover the commonly used methods for removing extra spaces in a string. So, let’s start.

Remove White Space in a String Using "isspace" Function

In MATLAB, the "isspace" function is a built-in function that is used to determine and remove the ASCII white space present in a string.

Syntax

string_without_space = isspace('input_string');

Example

Let us take an example in MATLAB programming to use this function to remove white spaces in a string.

% MATLAB code to remove white space from a string
% Specify a sample string with white space
S = 'Tutorials Point India Limited';

% Remove the white space using "isspace" function
string_without_spcaes = S(~isspace(S));

% Display the string with and without spaces
disp('The string with space: ');
disp(S);
disp('The string without space: ');
disp(string_without_spcaes);

Output

When your run this code, it will produce the following output

The string with space: 
Tutorials Point India Limited
The string without space: 
TutorialsPointIndiaLimited

Code Explanation

In this example, we start by defining a string containing white spaces. Then, we use the logical indexing and the "isspace" function to remove the space from the string. Finally, we use the "disp" function to display the string with and without whitespaces.

Remove White Space in a String Using "strrep" Function

In MATLAB, the "strrep" is another built-in function that can be used to remove white spaces from a string. This function uses the find and replace method to remove the whitespaces occurring in a string.

string_wo_spaces = strrep(S1, S2, S3);

Here, S1, S2, and S3 are strings. This function replaces all the occurrences of string "S2" within the string "S1" with the string "S3".

Example

Let us see an example to remove the whitespaces in a string using the "strrep" function in MATLAB.

% MATLAB code to remove white space from a string
% Specify a sample string with white spaces
S = 'Tutorials Point India Limited';

% Remove the white space using "strrep" function
string_without_spcaes = strrep(S, ' ', '');

% Display the string with and without spaces
disp('The string with space: ');
disp(S);
disp('The string without space: ');
disp(string_without_spcaes);

Output

When your run this code, it will produce the following output

The string with space: 
Tutorials Point India Limited
The string without space: 
TutorialsPointIndiaLimited

Code Explanation

In this example, we firstly create a string with white spaces and then call the "strrep" function to replace all the spaces with no space. At the last, we display the string with and without spaces using the "disp" function.

Remove White Space in a String Using "regexprep" Function

In MATLAB, the "regexprep" is a built-in function that can be used to remove the spaces occurring in a string.

Syntax

string_wo_spaces = regexprep(str, expression, replace);

Here, "str" is the string with spaces, "expression" is the regular expression to match the spaces in the string, and "replace" is the empty string or no space character.

Example

Let us see an example to understand how this function is used to remove white spaces in MATLAB.

% MATLAB code to remove spaces in a string
% Define a sample string with spaces
S = 'Tutorials Points India Limited';

% Specify the regular expression to match spaces
E = '\s+';

% Remove the spaces from the string
string_wo_spaces = regexprep(S, E, '');

% Display the string with and without spaces
disp('String with spaces: ');
disp(S);
disp('String without spaces: ');
disp(string_wo_spaces);

Output

When your run this code, it will produce the following output

String with spaces: 
Tutorials Points India Limited
String without spaces: 
TutorialsPointsIndiaLimited

Code Explanation

In this example, firstly we create a string containing spaces. Then, we define a regular expression pattern "E" to match the whitespaces occurring the string.

After that we call the "regexprep" function to replace the whitespace characters occurring in the input string with no space character. Finally, we display the strings with and without spaces using the "disp" function.

Remove White Space in a String Using "deblank" Function

The "deblank" is another built-in function in MATLAB to remove spaces in a string. It can be employed for removing the trailing spaces or tab spaces in a string.

Syntax

StringWithoutSpace = deblank(str);

Here, "str" is the input string with trailing or tab spaces.

Example

Let us see an example to understand the use of this function to remove spaces in a string.

% MATLAB code remove trailing tab spaces in a string
% Specify a string with training tab spaces
S = sprintf('\t Tutorials-Point \t');

% Display the string with '$' symbol for clarity
disp(['Input string: $' S '$']);

% Remove the trailing tab spaces from the string
string_wo_space = deblank(S);

% Display the string without trailing tab spaces
disp(['String without space: $' string_wo_space '$']);

Output

When your run this code, it will produce the following output

Input string:			$	 Tutorials-Point 	$
String without space: 	$	 Tutorials-Point$

Code Explanation

In this example, it can see that the "deblank" function has removed the trailing space from the string.

Remove White Space in a String Using "strtrim" Function

In MATLAB, there is another built-in function namely, "strtrim" that can be used to remove the leading and trailing spaces from a string.

Syntax

string_wo_spaces = strtrim(input-string);

Example

Let us see an example to understand how to remove the leading and trailing spaces from a string using the "strtrim" function in MATLAB.

% MATLAB code to remove leading and trailing spaces in a string
% Create an input string with leading and trailing spaces
S = sprintf('\t Tutorials-Point \t');

% Display the input string with '$' symbol for clarity
disp(['Input String: $' S '$']);

% Remove leading and trailing spaces
string_wo_spaces = strtrim(S);

% Display the output string without leading and trailing spaces
disp(['Output String: $' string_wo_spaces '$']);

Output

When your run this code, it will produce the following output

Input String:	$	 Tutorials-Point 	$
Output String:	$Tutorials-Point$

Explanation

In this example, it can be seen that the "strtrim" function has removed the leading and trailing spaces from the string.

Conclusion

This is all about removing spaces in a string using MATLAB. In MATLAB, there are various built-in functions that can be used to remove the whitespaces present in a string. In this tutorial, I explained all the commonly used functions to remove white spaces from a string.

Updated on: 09-Oct-2023

112 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements