In Matlab, is it possible to terminate a script, but save all its internal variables to workspace?

前端 未结 3 1295
不思量自难忘°
不思量自难忘° 2020-12-01 08:05

I am running a script, but it is taking much too long so I want to terminate the script. However it has calculated a lot of data which I would ideally not want to throw away

3条回答
  •  难免孤独
    2020-12-01 08:43

    A colleague showed me an alternate way to incorporate this in my function, by attaching a save() command to the cancellation of a waitbar like so:

    %appoint emergency file location
    emergencysave = char(inputdlg({'fill in here:'}, 'windowtitle', 1, 'c:\defaultstringhere.mat'));
    
    %or just emergencysave = 'c:\emergencysave.mat';
    
    
    
    %create some GUI element you can cancel
    times = 10;
    wbinfo = struct('curlength', {0.0});
    wb = waitbar(wbinfo.curlength);
    wbinfo.wb = wb;
    
    
    
    %attach save() to cancelling
    anyimportantvariable = [];
    for i=1:times
        anyimportantvariable = [anyimportantvariable, i^2];
        wbinfo.curlength = i/times;
        try
            waitbar(wbinfo.curlength, wb)
        catch
            save(emergencysave, 'anyimportantvariable');
            return;
    end
    

提交回复
热议问题