Have you a registration already ? Sign In
The Use Of ‘eval()’ Command In MatLab With Examples
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.
>> 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.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
Write Comments