function [contrast_array, periodX_array, periodY_array] = analyzeGSWavefunction_multipleruns(folder_base_path, numberofruns, save_option) % Extract the last two folder names from folder_base_path folder_parts = strsplit(folder_base_path, '/'); if length(folder_parts) >= 2 video_name_base = strcat(folder_parts{end-1}, '_', folder_parts{end}); else video_name_base = folder_parts{end}; % In case there's only one folder in the path end % Create the output video name with .mp4 extension (for video option) output_video_name = strcat(video_name_base, '_wavefunction_analysis.mp4'); % Create the output folder name (for image saving option) output_folder_name = strcat(video_name_base, '_wavefunction_analysis_images'); if strcmp(save_option, 'images') && ~exist(output_folder_name, 'dir') mkdir(output_folder_name); % Create the folder if it doesn't exist end % Initialize arrays to store contrast, periodX, and periodY for each run contrast_array = zeros(1, numberofruns); periodX_array = zeros(1, numberofruns); periodY_array = zeros(1, numberofruns); % Set up video writer if the user chooses 'video' if strcmp(save_option, 'video') videoObj = VideoWriter(output_video_name, 'MPEG-4'); % Create a video file object videoObj.FrameRate = 1; % Set the frame rate (adjust as needed) videoObj.Quality = 100; % Maximum quality (for compression) open(videoObj); % Open the video file for writing end % Loop through all the runs and process each folder for run_index = 1:numberofruns % Call the function that processes the wavefunction data [contrast, periodX, periodY] = Scripts.analyzeGSWavefunction(folder_base_path, run_index-1); % Store the results in the arrays contrast_array(run_index) = contrast; periodX_array(run_index) = periodX; periodY_array(run_index) = periodY; % Capture the current figure (output of analyzeGSWavefunction_multipleruns) frame = gcf; % Get current figure as a frame if strcmp(save_option, 'video') % Write the frame to the video file writeVideo(videoObj, getframe(frame)); elseif strcmp(save_option, 'images') % Save the figure as an image image_filename = fullfile(output_folder_name, sprintf('wavefunction_run_%d.png', run_index-1)); saveas(frame, image_filename); % Save the image as a PNG file (adjust file type if needed) end % Optionally close the figure if you don't need it close(gcf); end % Close the video writer if the user chose 'video' if strcmp(save_option, 'video') close(videoObj); fprintf('Video saved as %s\n', output_video_name); elseif strcmp(save_option, 'images') fprintf('Images saved in folder %s\n', output_folder_name); end % Return the arrays end