Saturday, September 17, 2016

Matlab: Concatenating string and integer

Here is how we can concatenate an integer to a string in matlab.

Let us say we have a string "test" and we want to generate the string test1,test2,test3 so on. We can do it by using the function strcat, which concatenates two strings passed as input to it. But as the numbers 1,2,3 etc are numbers we need to convert them to strings using the function int2str.

int2str takes an integer number as returns it by converting it into a string.



y will be equal to test1 as we converted 1 to string and concatenated it with the sting 'test'.

Example program 1



Ouput:



Example program 2



Output:


Wednesday, September 14, 2016

Matlab: Finding the number in an array nearest to a given number

We can use the following method in matlab to find a value in an array which is nearest to a given number.

Let us say we have an array "temp" with the following numbers



Now if we want to find the number closest to 32 in the array we can do the following.



Subtract the given number from all the number in the array



find the absolute value of the difference by using absolute



Now find the minimum number in the diff array by using the function min



The variable difference will have the minimum difference and index will point to the index of the number.

temp(index) will give us the actual number closest to the given variable.


Follow by Email