How To Insert A Value Into An Array Matlab
This tutorial will discuss inserting a variable value into a cord using the num2str()
function in MATLAB.
Insert Variable Into Cord in MATLAB
If we want to insert the value of a variable into a string, we demand to convert the value into a string or an assortment of characters using the num2str()
function, and so nosotros can concatenate it with the string using the strcat()
office. For case, allow us insert a floating-betoken number into a cord.
See the code below.
clc articulate north = 100.577; s1 = num2str(n); s2 = "value is "; s3 = strcat(s2,s1)
Output:
We can besides add precision or the number of digits we want to meet in the output using the 2nd statement of the num2str()
part, and its input should be a positive integer. The 2d statement 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 case, let's convert only the first 3 elements of the number into a string and concatenate it with the other string. See the code beneath.
clc clear due north = x.212; s1 = num2str(northward,3); s2 = "value is "; s3 = strcat(s2,s1)
Output:
In the output, the string s3
only contains 3 digits of the floating-betoken number because we have gear up the precision to 3 in the num3str()
office. If we don't set up the precision, the num2str()
function volition convert the unabridged number into an array of characters.
We can besides prepare the formatting type of the input like the number of significant digits in the instance of floating-bespeak numbers using the f
character with the percent sign, and nosotros can laissez passer the number of significant digits betwixt the percentage and f
grapheme. For example, let's round the long floating-point number to 2 significant digits and and then concatenate it with a cord.
See the code below.
clc clear n = 10.218; s1 = num2str(due north,'%0.2f'); s2 = "value is "; s3 = strcat(s2,s1)
Output:
We can see in the to a higher place output that the number is rounded to two significant digits. In the above code, we have divers the string using double quotation marks, but nosotros can too define a string using single quotation marks.
We can also concatenate multiple stings in the aforementioned line using the strcat()
part. We simply have to pass all the cord variables in the function.
Nosotros can also utilise the plus sign between two or more strings to concatenate them and get a single string. In the higher up code, we accept manually added a infinite between the number and the string, but if we ascertain the string with single quotations, we volition non be able to add a space betwixt the two variables.
For example, let'southward repeat the above case using single quotation marks. Meet the code below.
clc clear n = 10.218; s1 = num2str(n); s2 = 'value is '; s3 = strcat(s2,s1)
Output:
In the to a higher place output, we have added a space in the s2
cord, but the space is not present in the s3
cord. If we use the plus sign, the output will exist the same.
To avert this problem, at to the lowest degree 1 of the strings being concatenated should be a string, non a grapheme vector or array. The num2str()
role ever returns a graphic symbol array, but we can use the string()
part to become the output as a cord.
For instance, let's utilise the cord()
function in the above example. Encounter the code below.
clc clear n = 10.218; s1 = cord(n); s2 = 'value is '; s3 = s2 + s1
Output:
We can see that the space is nowadays 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 tin 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()
part. We can join variables of different information types using the join()
function, but we have to put all the variables in a unmarried vector or matrix and pass it inside the join()
function to join them.
For example, let's bring together a string, a character array, and a numeric value using the join()
role. See the code below.
clc clear due north = 100.55; s1 = "Value is"; s2 = 'kg'; north = [s1,n,s2]; s = join(n)
Output:
Note that at to the lowest degree one value of the variables we want to join should be a cord; otherwise, the result can change. In the higher up output, the three variables have been joined together; the result is saved as a string considering one input is a string.
We tin can also utilize the strjoin()
function, but the variables we want to join should be of character assortment data blazon and enclosed in an assortment. Past default, the join()
and strjoin()
functions put space between the values of the vector or array, but we tin can likewise put other delimiters and characters using the 2d argument of the join()
and strjoin()
functions.
We have to pass the delimiter as a character array inside the bring together()
or strjoin()
function to add them between the elements of the vector or array. For example, allow'south repeat the above case using a different delimiter.
Run into the lawmaking below.
clc articulate north = 100.55; s1 = "Value is"; s2 = 'kg'; n = [s1,n,s2]; south = bring together(n,'--')
Output:
s = "Value is--100.55--kg"
Nosotros can come across in the above output that the input delimiter is added to the output instead of the space delimiter. We can also add together a string or grapheme array of any length as a delimiter between the elements of the given matrix or array.
Nosotros can likewise utilise the sprintf()
function of Matlab, which is used to add formatted data into a string or character assortment. The first argument of the sprintf()
function is the format specs, and the adjacent arguments are the variables we want to put in a string or character array.
If the format specs are divers as a cord using double quotation marks, the output of the sprintf()
function volition also be a cord. If the format specs are defined as a character array using single quotation marks, the output of the sprintf()
function will besides be a graphic symbol array.
We tin use the format specs in the aforementioned way equally we used above in the instance of the num2str()
part. We can use the percent sign; later that, nosotros can pass the field width, so we can laissez passer the data type.
For example, let's add different information types variables to a cord using the sprintf()
function. Run across the code beneath.
clc articulate due north = 100.55; s1 = "Value is"; s2 = 'kg'; south = sprintf("%s %0.1f %s",s1,due north,s2)
Output:
In the above code, we used %s
for the string data blazon and %f
for the floating-betoken data type. We likewise divers the number of significant digits in the case of floating-point numbers.
In the above lawmaking, we saved the strings in variables, and so passed them in the sprintf()
function, but nosotros 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 within the sprintf()
function for simplicity.
We tin also use symbols for other data types like %c
for characters, %d
for integers, so on. Check this link for more details nigh the sprintf()
function.
Source: https://www.delftstack.com/howto/matlab/insert-variable-into-string-matlab/
0 Response to "How To Insert A Value Into An Array Matlab"
Post a Comment