1. First let me tell you this weird behavior. I have these two functions in one file, test_evalin.m.
% begin function test_evalin onCleanup(@()cleanup); x = 7 y = 3
function cleanup evalin('caller', 'whos') % end
It returns that both x and y are in the workspace. But if I add semi-colons to the end of the assignment lines (i.e. "x=7;"), then it returns that there are no variables in the workspace. For some reason, adding a semi-colon makes it not show up. Can anyone else confirm this behavior? And is it a bug, or does it make sense to you?
2. Ultimately, what I want to do is have my function give one value if it terminates with a Ctrl-C and another if it terminates normally. Here's my new sample code:
% begin function testf(handles) onCleanup(@()cleanup); handles.s('cat') = 3; x = 5; pause(3); x = 7;
function cleanup handles = evalin('caller', 'handles'); x = evalin('caller', 'x'); handles.s('dog') = x; % end
I call it like this: % begin handles = struct; handles.s = containers.Map(); testf(handles); values(handles.s) % end
So if it gets through the pause, x will be 7, and the cleanup function will assign that to the map. Otherwise, the old value of 5 will be assigned. This exhibits the same problem with the semi-colon as above. However, more broadly on my objective, does this look like a good approach? Or is there a better way to achieve this dichotomous behavior for when Ctrl-C happens.