function[trials] = calculate_statistics(trials) item_struct = struct('time', [], 'value', []); stats_struct = struct('max', item_struct, ... 'mean', [], ... 'median', [], ... 'mode', [], ... 'std', [], ... 'q1', [],... 'q2', [],... 'q3', [],... 'auc', []); for t = 1:length(trials) trial = trials(t); eyes = fieldnames(trial.data); for e = 1:length(eyes) try eye = eyes{e}; trial.stats.(eye) = stats_struct; signal = trial.data.(eye).baseline_corrected.filtered; if isempty(signal) continue; end % only relevant after stimulus onset trial_after_so = trial.data.(eye).baseline_corrected.filtered(find(trial.time > trial.stimulus_onset)); trial_before_so = trial.data.(eye).baseline_corrected.filtered(find(trial.time <= trial.stimulus_onset)); % calculate stats from trial trace max_after_so = max(trial_after_so); max_ind = length(trial_before_so) + find(trial_after_so==max_after_so); delta_t = trial.time(max_ind(1)) - trial.stimulus_onset; mean_after_so = mean(trial_after_so); std_after_so = std(trial_after_so); mode_after_so = mode(trial_after_so); median_after_so = median(trial_after_so); q1 = prctile(trial_after_so, 25); q2 = median_after_so; q3 = prctile(trial_after_so, 75); % store values in trial trial.stats.(eye).max.value = max_after_so; trial.stats.(eye).max.time = delta_t; trial.stats.(eye).median = median_after_so; trial.stats.(eye).mean = mean_after_so; trial.stats.(eye).std = std_after_so; trial.stats.(eye).mode = mode_after_so; trial.stats.(eye).q1 = q1; trial.stats.(eye).q2 = q2; trial.stats.(eye).q3 = q3; trial.stats.(eye).auc = trapz(trial_after_so); catch warning('No statistics generated for trial'); end end % store trial back in array trials(t) = trial; end end