exlux15
exlux15

Reputation: 939

Nested Parallel While Loops

I am trying to run two while loops in parallel for the purpose of data acquisition. This program will run for "n" num_trials with specific trial duration for each trial.

During each trial, the program will collect data while keeping track of the trial duration. For example, the first trial starts collecting data at 1 seconds and stops at 10 seconds. This process will repeat for the rest number of trials

How can I run both while loops in parallel?

Basically, I just want a method that will break the second while loop once the specified trial duration is complete?

Thanks in advance.

count = 0;
num_trials = 5; % Number of Trials = 5 Trials
trial_duration = 10; % Trial Duration = 10 Seconds

global check 
check = 0;

% Number of Trials
for i = 1:num_trials

    fprintf('Starting Trial %i\n', i);

    t_start = tic;

    % Start counting and Collecting Data
    while toc(t_start) < trial_duration

        % Data Collection
        while (check ~= 1)  
            count = count +1;
        end

    end

    fprintf('Ending Trial %i\n', i);
end

Upvotes: 0

Views: 119

Answers (1)

Dan
Dan

Reputation: 45752

Have you tried using a single loop with && ?

while (toc(t_start) < trial_duration) && (check ~= 1)

        % Data Collection
        count = count +1;

end

Upvotes: 1

Related Questions