matlabsavequit

Is there a command to stop a program and still save the data?


I am working on an experiment that I talk data and MatLab graphs the data and saves it in a video file.

For right now, I use a tic/toc function and while loop to control the duration for Matlab to record the data for the experiment. Sometimes, however, the experiment finishes faster than the time I set the timer on. So for a long time I have to wait for MatLab to finish recording data that I am not going to use anyway.

I am looking for a MatLab function or technique that could use to stop the program on command and still keep the data that it recorded so far.

Another thing is I don't know how long each experiment will take so I can't set a specific time. One experiment could be for a couple of secs another could be for more than two minutes.

The code I have right now is something like this:

tic;
while toc<90 % run loop until time is 90 secs
%Record data
%graph data
end
%save all data to a file

Solution

  • You could save the data in each iteration, and then break the program in the middle by using

    CTRL+C

    This might not be most efficient, but if it is feasible it should solve your problem.

    tic;
    while toc<90 % run loop until time is 90 secs
       %Record data
       %graph data
       %save all data to a file
    end
    

    Another method would be checking for specific keypress in the loop, and then save all data

    tic;
    while toc<90 % run loop until time is 90 secs
       %Record data
       %graph data
       %if user pressed Q
          %save all data to a file
          break
       %
    end
    

    To determine whether a key was pressed:

    key = get(gcf,'CurrentKey');
    if(strcmp (key , 'return'))
        % Do something
    end