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.

采纳的回答

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 中查找有关 Whos 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by