% Clear all variables, clear command window, and close all figures
clear all;
clc;
close all;

% Specify the folder path
folder_path = 'C:\Users\CTUCARTECH\OneDrive - České vysoké učení technické v Praze\Dokumenty\1-SKOLA\3-VS\masters\diploma_thesis\!process\psd\matlab\matlab_psd_evaluation\scripts\psd_axis_comp';

% Get a list of all .txt files in the subfolder
file_list = dir(fullfile(folder_path, '*.txt'));

% Initialize cell arrays to store data
data = cell(1, length(file_list));

% Loop through each file
for i = 1:length(file_list)
    % Read data from the current file
    file_path = fullfile(folder_path, file_list(i).name);
    
    % Check the file size
    file_info = dir(file_path);
    if file_info.bytes == 0
        fprintf('Warning: Empty file %s\n', file_list(i).name);
        continue;  % Skip empty files
    end
    
    try
        file_data = readmatrix(file_path, 'NumHeaderLines', 2, 'Delimiter', ',');
    catch
        fprintf('Error reading file %s\n', file_list(i).name);
        continue;  % Skip files with read errors
    end
    
    % Check if the data has the expected format
    if size(file_data, 2) ~= 2
        fprintf('Warning: Unexpected data format in file %s\n', file_list(i).name);
        continue;
    end
    
    % Delete the first row
    file_data = file_data(2:end, :);
    
    % Save the data as variables
    freq_var = file_data(:, 1);
    amp_var = file_data(:, 2);
    
    % Store variables in the data cell array
    data{i} = {freq_var, amp_var};
end

% Create plots
colors = {'b', 'r', 'g', 'c', 'm', 'y', 'k', 'b', 'r'};
labels = {'psd_brf_x', 'psd_brf_y', 'psd_brf_z', 'psd_brl_x', 'psd_brl_y', 'psd_brl_z', 'psd_brr_x', 'psd_brr_y', 'psd_brr_z'};

% Create figures for each column
figure;
for i = 1:3
    subplot(3, 1, i);
    for j = i:3:length(data)
        plot(data{j}{1}, data{j}{2}, colors{j}, 'LineWidth', 1.5);
        hold on;
    end
    xlabel('Frequency (Hz)');
    ylabel('PSD (g^2/Hz)');
    title(['PSD Plot for Column ', num2str(i)]);
    legend(labels{i:3:end});
    xlim([5, 120]);   % Set x-axis limits
    ylim([0, 0.01]);  % Set y-axis limits
    hold off;
    
    % Save the figure in the "psd_axis_comp" subfolder as PNG
    saveas(gcf, fullfile('psd_axis_comp', ['PSD_Column_', num2str(i), '_Figure.png']));
end

% Create plots
colors = {'b', 'r', 'g', 'c', 'm', 'y', 'k', 'b', 'r'};
labels = {'psd_brf_x', 'psd_brf_y', 'psd_brf_z', 'psd_brl_x', 'psd_brl_y', 'psd_brl_z', 'psd_brr_x', 'psd_brr_y', 'psd_brr_z'};

figure;

for i = 1:length(data)
    subplot(3, 3, i);
    plot(data{i}{1}, data{i}{2}, colors{i}, 'LineWidth', 1.5);
    xlabel('Frequency (Hz)');
    ylabel('PSD (g^2/Hz)');
    title(labels{i});
    xlim([5, 120]);   % Set x-axis limits
    ylim([0, 0.01]);  % Set y-axis limits
    
    % Save the figure in the "psd_axis_comp" subfolder as PNG
    saveas(gcf, fullfile('psd_axis_comp', ['PSD_', labels{i}, '_Figure.png']));
end


%% ... (Previous code)

% Create a figure for each subplot in the first column
for i = 1:3
    % Create a new figure
    fig = figure('Name', ['PSD ', upper(labels{i}(end-1:end)), '-Axis']); % Set the figure name
    
    % Plot the i-th subplot from the original plot in the new figure
    subplot(1, 1, 1);
    for j = i:3:length(data)
        if i == 1  % Set specific colors for the first plot
            plot(data{j}{1}, data{j}{2}, colors{mod(j-1, length(colors)) + 1}, 'LineWidth', 1.5);
        else
            plot(data{j}{1}, data{j}{2}, colors{j}, 'LineWidth', 1.5);
        end
        hold on;
    end
    
    % Customize the new figure as needed
    xlabel('Frequency (Hz)');
    ylabel('PSD (g^2/Hz)');
    title(['PSD Plot for ', upper(labels{i}(end-1:end)), '-Axis']);
    legend(labels{i:3:end});
    xlim([5, 120]);   % Set x-axis limits
    ylim([0, 0.01]);  % Set y-axis limits
    hold off;

    % Save the figure in the "psd_axis_comp" subfolder as PNG
    saveas(fig, fullfile('psd_axis_comp', ['PSD_', upper(labels{i}(end-1:end)), '_Axis_Figure.png']));
end
