필터 지우기
필터 지우기

Interactive script to return from a matlab function

조회 수: 2 (최근 30일)
Leo Simon
Leo Simon 2016년 7월 6일
댓글: Leo Simon 2016년 7월 6일
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.

채택된 답변

Richard Fisher
Richard Fisher 2016년 7월 6일
Have a look at evalin, and use it in your myReturn function as:
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
  댓글 수: 2
Stephen23
Stephen23 2016년 7월 6일
편집: Stephen23 2016년 7월 6일
The second solution is the correct and most robust way to do this, but it still won't have the effect that you think it will (see Walter Roberson's answer).
It is best to avoid using eval and evalin for such trivial code:
Leo Simon
Leo Simon 2016년 7월 6일
Thanks,the if myReturn approach works fine, requires a couple more lines than I wanted but does solve the problem.

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

추가 답변 (1개)

Walter Roberson
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.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by