To show different variable values in different forms such as string, number, or integer, you can use character and number interchange codes in MatLab®. We explained the use of different commands available in Matlab® to change characters between themselves.
Take a look at the examples below.
>> x = [1.5, 2.5, 3.52]; int2str(x) ans = '2 3 4' >>
All the example codes are executed in the Matlab® command window. Also, you can execute these codes in your license of Matlab®.
As you see in the example above, we created a vector that includes decimal numbers. We can use int2str() command to round these values to the nearest integer and change them into a set of strings.
In answer, the numbers are shown as string, inside quotes.
>> a = input('enter your age'); disp(['your age is ', num2str(a) ,'.']); enter your age25 your age is 25. >>
To use a integer value inside disp() command, you need to convert it to string from integer by using ‘num2str()’ command. You can directly place the num2str() inside disp() or fprintf() command in Matlab®.
>> a=str2num('3e3-6i') a = 3.0000e+03 - 6.0000e+00i >>
You can type the number as a string just inside ‘a’ vector in the example above. To do it, you need to use the str2num() command. You can see the result that is created as an integer.
>> a=['I',blanks(20),'love you...'] a = 'I love you...' >>
Just num2str() or other commands, you can place the blanks() command inside disp() or fprintf() commands in Matlab®. The number inside brackets of blanks() command defines the number of blanks to be created inside the disp() command.
You can see the effect in the result above.
>> a=['Mechanical Base is a very lovely website...']; findstr(a,'lovely') ans = 27 >>
In the example above, we defined a string vector ‘a’ in Matlab®. Then we want to find the place of the ‘lovely’ word inside the full text of ‘a’. What we did is we just placed ‘a’ and the word inside the brackets of findstr() command as you see above.
So the word ‘lovely’ starts from 27th place in the whole text.
>> a=['Mechanical Base is a very lovely website...']; lower(a) ans = 'mechanical base is a very lovely website...' >>
We created a string vector again in Matlab®. If you use the ‘lower()’ command as you see above and place a vector inside brackets, all letters will be lowercase as you see at the answer.
>> a=['Mechanical Base is a very lovely website...']; upper(a) ans = 'MECHANICAL BASE IS A VERY LOVELY WEBSITE...' >>
The same thing in an inverse way is also valid for the ‘upper()’ command in Matlab®. Check the example above.
This is also a very useful feature of Matlab®. You can compare the sameness of texts that are defined inside string vectors. Check the examples below.
>> a=['Mechanical Base is a very lovely website...']; b=['Mechanical Base is a very lovely website...']; c = strcmp(a,b) d =['mechanical Base is a very lovely website...']; f = strcmp(a,d) c = logical 1 f = logical 0 >>
We created two strings that include the same texts ‘a’ and ‘b’. If we compare them by using the ‘strcmp()’ command that we typed inside brackets, the answer ‘c’ is logical 1. This means the two strings or texts are the same.
We created another string vector quite different from the other which is named ‘d’. If we compare ‘d’ with ‘a’, we get the answer of logical 0. It also means these two strings are not the same as each other.
YOU CAN LEARN MatLab® IN MECHANICAL BASE; Click And Start To Learn MatLab®!
These are the most general string and integer converting and assessing commands in Matlab®. If you know further commands, please leave your knowledge in the comments below to share with everyone.
If you want further examples about string and character codes in Matlab®, inform us in the comments below.
Do not forget to leave your comments and questions below about string and character codes in Matlab® below.
Your precious feedbacks are very important to us.