Search code examples
functionmatlabreturn-valueterminate

Matlab: Abort function call with Strg+C but KEEP return value


I have a function in matlab with something like this:

function [ out ] = myFunc(arg1, arg2)
    times = [];
    for i = 1:arg1
        tic
        % do some long calculations
        times = [times; toc];
    end

    % Return
    out = times;
end

I want to abort the running function now but keep the values of times which are currently already taken. How to do it? When I press strg+c, I simply loose it because it's only a local function variable which is deleted when the function leaves the scope... Thanks!


Solution

  • Simplest solution would be to turn it from a function to a script, where times would no longer be a local variable.

    The more elegant solution would be to save the times variable to a .mat file within the loop. Depending on the time per iteration, you could do this on every loop, or once every ten loops, etc.