Matlab

How to Remove Space in a String in MATLAB? 5 Ways

Strings are the building blocks of all programming languages and they are widely used to perform many tasks. MATLAB is a high-performance programming language and it allows us to perform many string operations. One such operation is removing spaces in a string which can be easily performed using various MATLAB built-in functions.

If you are not familiar with how to remove spaces in a string in MATLAB, follow this blog to address this query.

How to Remove Spaces from a String in MATLAB?

Removing spaces in a string can be easily done by implementing various MATLAB’s built-in functions that are given below:

1: How to Eliminate Spaces from a String Using the strtrim() Function?

The strtrim() is a built-in MATLAB function that allows us to remove leading and trailing whitespaces in a string. This function takes a string as an input argument and provides a new string having no leading and trailing spaces.

Syntax

The strtrim() function’s syntax is given below:

strtrim(str)

 

Example

In this example, we remove the leading and trailing spaces from the given string str using the strtrim() function in MATLAB.

Str = sprintf('\t Welcome to linuxhint \t')
new_Str = strtrim(Str)

 

2: How to Remove Spaces in a String Using the erase() Function?

The erase() is MATLAB’s built-in function that is used for removing all occurrences of the given match. This function can be used for removing spaces from a string by specifying space “ ” as a match. This function accepts a string and a match as arguments and returns a manipulated string having no match.

Syntax

The erase() function’s syntax is given below:

erase(str,match)

 

Example

This example removes all spaces from the given string str using the erase() function in MATLAB.

Str = 'Welcome to linuxhint'
new_Str = erase(Str, " ")

 

3: How to Remove Spaces in a String Using the deblank() Function?

The deblank() is a built-in function in MATLAB that enables us to remove the trailing white spaces and null characters from the given string. This function takes a string as an input argument and provides a modified string having no trailing white spaces and a null character.

Note: It is important to note that the deblank() function doesn’t remove the whitespaces that appear at the beginning of a string.

Syntax

The deblank() function’s syntax is given below:

deblank(str)

 

Example

In this MATLAB code, we remove the trailing spaces from the given string str using the deblank() function in MATLAB.

Str = sprintf('\t Welcome to linuxhint \t')
new_Str = deblank(Str)

 

4: How to Remove Spaces in a String Using the regexprep() Function?

The regexprep() is a built-in MATLAB function that allows us to manipulate a string using the given regular expression. This function accepts a string, a regular expression, and a replaced text as arguments and returns a new modified string with no spaces.

Syntax

The regexprep() function’s syntax is given below:

regexprep(str,expression,replace)

 

Example

In this example, we use MATLAB’s regexprep() function to remove spaces from the given string str.

Str = 'l i n u x h i n t'
new_Str = regexprep(Str,'\s+','')

 

5: How to Remove Spaces in a String Using the strrep() Function?

The strrep() is a built-in function in MATLAB used for finding and replacing the substrings in the given string. This function can also be used for removing spaces from a string by finding a string of spaces and replacing it with a string having no space. This function accepts a string, a substring1 (used for finding occurrences), and a substring2 (used for replacing the occurrences) as arguments and returns the manipulated string.

Syntax

The strrep() function’s syntax is given below:

strrep(str,substr1,substr2)

 

Example

The given MATLAB code uses the strrep() function to remove spaces from the given string str.

Str = 'l i n u x h i n t'
new_Str = strrep(Str,' ','')

 

Conclusion

MATLAB is a beneficial high-performance programming tool having a huge library of built-in functions to perform different string operations. One such operation is removing spaces in a string. This guide has provided five built-in functions strtrim(), erase(), deblank(), regexprep(), and strrep() to perform this task using some examples.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.