Extract Values From a String Using sscanf() in MATLAB

This tutorial will discuss scanning a string according to a given format using the sscanf() function in MATLAB.

Extract Values From a String Using the sscanf() Function in MATLAB

The sscanf() function scans a string according to a given format in MATLAB. For example, if the given string contains numbers and we want to separate them from each other and save them in a matrix, we can use the sscanf() function.

The first argument of the sscanf() function is the input string, and the second argument is the format that we want to use to scan the string like %d for numeric values. The third argument is optional and defines the size for the scanning of the string, and it is useful when we do not want to scan the entire string.

If we don’t specify the size, the function will scan the whole string. The sscanf() function returns four arguments.

The first output argument is the matrix which will contain the result of the scanning of the string. The second output argument is optional and returns the number of elements the function has successfully scanned.

The third optional output argument returns the error message that occurred during the scanning of the string. The fourth argument is also optional, and it returns the following index of the position at which the sscanf() stopped the scanning process.

For example, let’s create a string containing multiple floating-point numbers and separate them using the sscanf() function in MATLAB.

Code:

clc
clear

My_S = "3.1 2.2 5.2";
num = sscanf(My_S,'%f')

Output:

num =

    3.1000
    2.2000
    5.2000

In the above code, the clc command is used to clear the MATLAB command window, and the clear command is used to clean the workspace. As we see in the output, all the floating-point numbers have been separated and saved inside a matrix.

Now, let’s define another string that contains characters and numbers and try to separate the numbers.

Code:

clc
clear

My_S = "3.1 2.2 hello 5.2";
[num, count, msg, index] = sscanf(My_S,'%f')

Output:

num =

    3.1000
    2.2000


count =

     2


msg =

    'Matching failure in format.'


index =

     9

The above code defined four output arguments to store all the outputs from the sscanf() function. In the above result, the num variable contains two elements only, but as we can see, there are four numbers in the given string.

The reason is returned in the msg variable, which says there is a matching failure in the specified format. We set in the above code that we want to scan the floating-point numbers using the %f symbol, so when the function moves to the non-floating point number, which is the character h, it stops there.

It returned the error message along with the index, at which it stopped scanning the string, which is 9 in the output.

We can also define the size as the third input argument inside the sscanf() function. The size is related to the count in the above output; if we define the size as 1, the function will only read the first element of the string.

We can also define the string as m*n in the case of a matrix. This is useful if we only want to read a given matrix’s specific row or column.

We can use different kinds of formats to scan the given string like %d for signed integer or base 10 numbers, %u for unsigned integer or base 10 numbers, %o for octal or base 8 numbers, %x for hexadecimal or base 16 numbers, and %f for floating-point numbers.

Check this link for more details about the sscanf() function.

Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - MATLAB String

  • Convert Number to String in MATLAB
  • Escape Character Sequences on a String in MATLAB
  • Concatenate String in MATLAB