I have a script which might be aborted due to long runtime and continued later. Let's assume it looks a bit like this:
data = []; % can't preallocate as I don't know the number of entries yet here...
while(1)
% ...
data = [data; someNewEntry];
end
The good thing about it is that when running as a script, whenever I abort it, I have the variable data
in te workspace.
But I wanted to convert it into a function because the script has quite a big amount of variables and clutters up my workspace with it. Let's assume I now converted it like this:
function data = myFnc()
data = []; % can't preallocate as I don't know the number of entries yet here...
while(1)
% ...
data = [data; someNewEntry];
end
Problem nnow is: When I abort the function, I'm losing all the entries in data
which have been made to this point. How to solve this issue and make return the current vector data
when aborting the function? The only possible solution I came up with was to use was something like this to use in the for-loop:
if(nargout == 1)
assignin('caller','data', data);
end
But somehow I don't like this approach too much. But okay it seems alright to me. But one thing still annoys me about it: When using that I always assign the data to the workspace-var data
as I don't know how to get the name of the output variable of the caller (i.e. bla = myFnc()
-> it would be bla
, thus assignin('caller','bla', data);
). I know there is the matlab function inputnames()
but I couldn't find the equivalent for the output vars.
Thanks a lot in advance!