Insert Variable Into String in MATLAB

Insert Variable Into String in MATLAB

This tutorial will discuss inserting a variable value into a string using the num2str() function in MATLAB.

Insert Variable Into String in MATLAB

If we want to insert the value of a variable into a string, we need to convert the value into a string or an array of characters using the num2str() function, and then we can concatenate it with the string using the strcat() function. For example, let us insert a floating-point number into a string.

See the code below.

clc
clear

n = 100.577;
s1 = num2str(n);
s2 = "value is ";
s3 = strcat(s2,s1)

Output:

s3 =

    "value is 100.577"

We can also add precision or the number of digits we want to see in the output using the second argument of the num2str() function, and its input should be a positive integer. The second argument sets the maximum number of digits that will be converted into a string, and the input should be a floating-point number if we want to set the precision.

For example, let’s convert only the first 3 elements of the number into a string and concatenate it with the other string. See the code below.

clc
clear

n = 10.212;
s1 = num2str(n,3);
s2 = "value is ";
s3 = strcat(s2,s1)

Output:

s3 =

    "value is 10.2"

In the output, the string s3 only contains 3 digits of the floating-point number because we have set the precision to 3 in the num3str() function. If we don’t set the precision, the num2str() function will convert the entire number into an array of characters.

We can also set the formatting type of the input like the number of significant digits in the case of floating-point numbers using the f character with the percentage sign, and we can pass the number of significant digits between the percentage and f character. For example, let’s round the long floating-point number to 2 significant digits and then concatenate it with a string.

See the code below.

clc
clear

n = 10.218;
s1 = num2str(n,'%0.2f');
s2 = "value is ";
s3 = strcat(s2,s1)

Output:

s3 =

    "value is 10.22"

We can see in the above output that the number is rounded to two significant digits. In the above code, we have defined the string using double quotation marks, but we can also define a string using single quotation marks.

We can also concatenate multiple stings in the same line using the strcat() function. We only have to pass all the string variables in the function.

We can also use the plus sign between two or more strings to concatenate them and get a single string. In the above code, we have manually added a space between the number and the string, but if we define the string with single quotations, we will not be able to add a space between the two variables.

For example, let’s repeat the above example using single quotation marks. See the code below.

clc
clear

n = 10.218;
s1 = num2str(n);
s2 = 'value is ';
s3 = strcat(s2,s1)

Output:

s3 =

    'value is10.218'

In the above output, we have added a space in the s2 string, but the space is not present in the s3 string. If we use the plus sign, the output will be the same.

To avoid this problem, at least one of the strings being concatenated should be a string, not a character vector or array. The num2str() function always returns a character array, but we can use the string() function to get the output as a string.

For example, let’s use the string() function in the above example. See the code below.

clc
clear

n = 10.218;
s1 = string(n);
s2 = 'value is ';
s3 = s2 + s1

Output:

s3 =

    "value is 10.218"

We can see that the space is present in the output between the number and the string, and the output is also in the string data type. If we have a long array of characters or strings and want to join them and include a space or another delimiter between them, we can use the join() and strjoin() functions.

We can use the join() function if the values we want to join are in a vector or matrix, and if the values are in an array, we have to use the strjoin() function. We can join variables of different data types using the join() function, but we have to put all the variables in a single vector or matrix and pass it inside the join() function to join them.

For example, let’s join a string, a character array, and a numeric value using the join() function. See the code below.

clc
clear

n = 100.55;
s1 = "Value is";
s2 = 'kg';
n = [s1,n,s2];
s = join(n)

Output:

s =

    "Value is 100.55 kg"

Note that at least one value of the variables we want to join should be a string; otherwise, the result can change. In the above output, the three variables have been joined together; the result is saved as a string because one input is a string.

We can also use the strjoin() function, but the variables we want to join should be of character array data type and enclosed in an array. By default, the join() and strjoin() functions put space between the values of the vector or array, but we can also put other delimiters and characters using the second argument of the join() and strjoin() functions.

We have to pass the delimiter as a character array inside the join() or strjoin() function to add them between the elements of the vector or array. For example, let’s repeat the above example using a different delimiter.

See the code below.

clc
clear

n = 100.55;
s1 = "Value is";
s2 = 'kg';
n = [s1,n,s2];
s = join(n,'--')

Output:

s =

    "Value is--100.55--kg"

We can see in the above output that the input delimiter is added to the output instead of the space delimiter. We can also add a string or character array of any length as a delimiter between the elements of the given matrix or array.

We can also use the sprintf() function of Matlab, which is used to add formatted data into a string or character array. The first argument of the sprintf() function is the format specs, and the next arguments are the variables we want to put in a string or character array.

If the format specs are defined as a string using double quotation marks, the output of the sprintf() function will also be a string. If the format specs are defined as a character array using single quotation marks, the output of the sprintf() function will also be a character array.

We can use the format specs in the same way as we used above in the case of the num2str() function. We can use the percent sign; after that, we can pass the field width, and then we can pass the data type.

For example, let’s add different data types variables to a string using the sprintf() function. See the code below.

clc
clear

n = 100.55;
s1 = "Value is";
s2 = 'kg';
s = sprintf("%s %0.1f %s",s1,n,s2)

Output:

s =

    "Value is 100.5 kg"

In the above code, we used %s for the string data type and %f for the floating-point data type. We also defined the number of significant digits in the case of floating-point numbers.

In the above code, we saved the strings in variables, then passed them in the sprintf() function, but we can also directly write strings in the function’s first argument. In the case of large string variables, we should save them in variables and pass them inside the sprintf() function for simplicity.

We can also use symbols for other data types like %c for characters, %d for integers, and so on. Check this link for more details about the sprintf() function.

Author: Ammar Ali
Ammar Ali avatar Ammar Ali avatar

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

LinkedIn Facebook

Related Article - MATLAB String

  • MATLAB Line Continuation
  • Convert Number to String in MATLAB
  • Extract Values From a String Using sscanf() in MATLAB
  • Escape Character Sequences on a String in MATLAB
  • Concatenate String in MATLAB