Interactive script to return from a matlab function
조회 수: 1 (최근 30일)
이전 댓글 표시
I'm trying to construct a matlab script that I can call from a matlab function, which will prompt me if I want to return to the base workspace. Here's a simple version of my code, called myReturn.m
askIfIwantToReturn = input('Type r or R to return to base workspace \n','s');
if strncmpi(askIfIwantToReturn,'r',1)
timerOne=timer('StartDelay',0.01,'TimerFcn',@(src,evt) eval('return'));
%timerOne=timer('StartDelay',0.01,'TimerFcn',@(src,evt) disp('return'));
start(timerOne);
end;
Here's an example of how I wanted to use it, and how it fails.
function wrapper
disp('FOO');
myReturn;
disp('Back in caller workspace');
pause(1);
disp('BAR');
keyboard;
The plan is that the use of timer would allow me to exit from the workspace "myReturn" and then execute the return from the wrapper function's workspace, before the keyboard is reached, thus taking me back to the base workspace. Part of the plan works: to see this, uncomment the second, commented out specification of timerOne. The code then does indeed return to the caller workspace, execute the line
disp('Back in caller workspace');
then as intended, displays the word return, presumably from the caller workspace. So the problem is just that I can't get the return command to work.
any help would be most appreciated.
댓글 수: 0
채택된 답변
Richard Fisher
2016년 7월 6일
evalin('caller','return')
Alternatively, you could change your myReturn script to a function which returns a boolean if the user chooses to exit the function, then call it like this from your wrapper function:
if myReturn, return, end
추가 답변 (1개)
Walter Roberson
2016년 7월 6일
The code for timers is feval()'d by MATLAB in the base workspace. The "caller" for that would be some completely internal function. There is no way to tie the execution of a timer to some kind of "continuation" (in the Lambda Calculus sense) at the line of code that created the timer (nor to the line of code that set the callback function for the timer.) Think of it as having created a detached task to execute from the base workspace. The timer will execute whether or not the function that created the timer has already returned. There is no way for the timer to "inject" commands into the execution stream of some function.
If these are not the properties that you want, then you should instead not create a timer and instead should figure out how long you need to pause() until the timer would need to start, do that pause(), and then continue execution all in the same function.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Whos에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!