Return control form a function to the main program

조회 수: 3 (최근 30일)
Dhaval
Dhaval 2011년 6월 13일
So I have a function (say xyz.m) with inputs and outputs (integers and strings). I specify an 'if' condition, and if it not satisfied, then I want my program to discontinue executing any more lines from the function (xyz.m) and return the control to the very next line of my main program (let us call the main function as main.m). I tried using the syntax 'return' but it gives me an error because the variables that were supposed to be returned/filled by the function are not assigned (like this: ??? Output argument variable (and maybe others) not assigned during call <function.). So is there any other sytanx or command that does the job (something as simple as 'break' which simply quits the 'for' or 'while' loop). Thanks in advance.

채택된 답변

Matt Fig
Matt Fig 2011년 6월 13일
Define default output variables before the return statement.
function x = myx(A)
if A/2 > 7
x = [];
return
else
x = A+3;
end
  댓글 수: 1
Dhaval
Dhaval 2011년 6월 15일
Thank you very much, your solution is simple and works very well! But for some reason the value of x (since it is a null set) is displayed even though I've put semi-colon at the end of it.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Fangjun Jiang
Fangjun Jiang 2011년 6월 13일
Even if such command or function exists, you still have the problem that the return variables of your xyz.m have not been set yet. xyz.m may have multiple return variables such [x, y,z]=function xyz. A good practice is to initialize or set the default value for x, y and z at the begining of your xyz.m so you won't have that problem.
  댓글 수: 1
Dhaval
Dhaval 2011년 6월 15일
Yes, I understand. I was hoping for a command that would set the return variables as null sets by default. But thanks for the advice, I'll set the default value for the return variables in the beginning of the program.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by