Like ‘continue’ and ‘break’ commands that are generally used inside ‘if-else’ queries in Matlab, ‘return’ is an also very important and useful command. You can use ‘return’ if you want to exit from the sub-program if a statement is true. You can undertstand the use of ‘return’ at below example.
How To Use ‘return’ In MatLab?
>> x = [ 2 6 4 6 4 6 4 9 3 10 0 2 96 3]; for y=1:length(x) %number of loops equals to the elements of x if x(y) == 0 return end end y y = 11 %11. element of 'x' is the first 0. >>
As you see above, the use of ‘return’ is very basic in Matlab. You do not need to use any special syntaxes for it.
Assume that we want to create a Matlab program that finds the element adress inside a vector which equals to 0. To do it, first we nee to create a ‘for-end’ loop for each elements of vector ‘x’.
Inside this for-end loop, we placed an if-else query which includes the code that says, ‘if the value of this element of ‘x’ equal 0, exit from for-end loop’. In each loop, this query works for each elements of ‘x’. When the first element that is 0 is found, it halts the for-end loop.
Then we can take the element adress by typing the value of ‘y’ into command window in Matlab as above.
The use of ‘return’ in Matlab is very easy like above. If you want further examples about the use of ‘return’ in Matlab, please inform us from comments.
Do not forget to leave your comments and questions about the use of ‘return’ command in Matlab programming below.
Your precious feedbacks are very important for us.
Leave a Reply