Interactive script to return from a matlab function
2 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Richard Fisher
on 6 Jul 2016
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
More Answers (1)
Walter Roberson
on 6 Jul 2016
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 Comments
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!