0

I am creating a GUI in Matlab that will read and write data to a text file based on the trial duration. The user will enter the number of trials and trial duration and then push the "start" button.

For example the user enters 5 trials at 10 seconds duration. While starting the 1st trial, I will need to read/write data continuously for 10 seconds, then stop and save the text file. This process will continue for the next 5 trials. Here is a brief code I tried to implement below.

How can I run both the timer for 10 seconds and simultaneously read/write data with that time limit?

Thanks in advance.

        % Get Number of Trials
        number_trials = str2double(get(handles.Number_Trials,'String'));
        % Get Trial Duration
        trial_duration = str2double(get(handles.Trial_Duration,'String'));

        % Timer Counter
        global timer_cnt
        timer_cnt = 0;

        global eye_data
        eye_data = 0;

        for i = 1:number_trials
            % Set Current Trial Executing
            set(handles.Current_Trial_Text,'String',num2str(i));

            % Set Text File Specifications
            data_fname = get(handles.Data_Filename_Edit_Text,'String');
            file_fname = '.dat';
            data_fname_txt = strcat(data_fname,file_fname);

            % Timer Object
            fprintf('%s\n','Timer Started');

            % Pauses 10 Seconds
            t = timer('TimerFcn','stat=false','StartDelay',10);

            start(t);

            stat = true;
            while(stat == true)
                disp('.');
                pause(1)
            end

            fprintf('%s\n','Timer Ended');
            delete(t);

        end
exlux15
  • 909
  • 1
  • 9
  • 16
  • 2
    so.. what's your question? – Dang Khoa Jul 08 '13 at 18:50
  • @DangKhoa Editted Post with Question. Thanks. – exlux15 Jul 08 '13 at 19:07
  • Okay, so let me make sure I understand: in your example, the user specifies the number of trials he wants, and the length of time per trial. Then, a timer will collect data until the time is up. It does this for `num_trials` times. – Dang Khoa Jul 08 '13 at 22:38
  • @DangKhoa Yes that is what I am trying to accomplish. I tried your answer (tic/toc) but that only executes for 10 seconds for 1 trials. How can do this "n" trials? – exlux15 Jul 09 '13 at 20:06
  • Wrap it in a for loop. I thought that would have been fairly obvious. – Dang Khoa Jul 10 '13 at 00:16
  • @DangKhoa I wrapped in a for loop, it completes the first trial in 10 seconds then executes the rest of trials iteratively. How can I reset tic/toc to 0 after completing each trial? Thanks. – exlux15 Jul 10 '13 at 02:45
  • How did you call it? `t_start = tic` needs to be inside your `for` loop to get reset. Basically the entire code snippet I posted should be in your `for` loop. – Dang Khoa Jul 10 '13 at 05:58

1 Answers1

1

In my experience, timers are usually used in the context of "wait this amount of time, then do foo" rather than how you're using it, which is "do foo until you've done it for this amount of time".

The humble tic/toc functions can do this for you.

t_start = tic;
while toc(t_start) < 10
    % do data collection
end
Dang Khoa
  • 5,693
  • 8
  • 51
  • 80
  • Note that you can either place both `tic` and `toc` in the loop, or compare `toc` with the required amount of seconds multiplied by your loop variable. `toc < 10 * i`. Also note that assigning `tic` to a variable and calling `toc` with this variable as input is optional. – Dennis Jaheruddin Jul 11 '13 at 12:48