admin管理员组

文章数量:1123101

I would like to modify my below Matlab code to load three .mat files in MATLAB and do the same thing as only for one, but all of them at once and plot their results for comparison. The files represent data for three angles: 15°, 30°, and 45°, and each file contains variables like berMap, coveragestrong text, and QAM_order.

The process involves calculating metrics like:

1- The relative difference in coverage area.

2- The relative difference in bit error rate (BER) (90th percentile).

3- Coverage evolution for both a "perfect case" and a "misalignment case."

Expected Outcome: I want to:

1- Load the three files.

2- Process the data in a loop.

3- Generate plots comparing the results for all three angles.

Any assistance, please?

The files that I need load them at once:

workspace_withBeamsteering_15.mat
workspace_15_withBeamsteering_Misalignment.mat

workspace_withBeamsteering_30.mat
workspace_30_withBeamsteering_Misalignment.mat

workspace_withBeamsteering_45.mat
workspace_45_withBeamsteering_Misalignment.mat

This is my code:

clear variables;
close all;
clc;

%% DATA PROCESSING
% Load data for the perfect beamsteering case
load workspace_withBeamsteering_15.mat; % Replace with the actual file path
berMapPerfectCase = berMap;
coveragePerfectCase = coverage;

% Load data for the beamsteering with misalignment case
load workspace_15_withBeamsteering_Misalignment.mat; % Replace with the actual file path

% Initialize variables
maxBerMap = cell(1, size(QAM_order, 1));
berDiff = cell(1, size(QAM_order, 1));
berRelativeDiff = cell(1, size(QAM_order, 1));
cdf90_berDiff = zeros(1, size(QAM_order, 1));
cdf90_berRelativeDiff = zeros(1, size(QAM_order, 1));
indexCoverage = cell(1, N_n_t);
coverage = zeros(1, size(QAM_order, 1));
coverageDiff = zeros(1, size(QAM_order, 1));
coverageRelativeDiff = zeros(1, size(QAM_order, 1));

% Initialize storage for coverage and intersection maps
coverageMaps = cell(size(QAM_order, 1), 1);
intersectionMaps = cell(size(QAM_order, 1), 1);
intersectionAreas = zeros(size(QAM_order, 1), 1); % Store intersection areas for comparison

for q = 1:size(QAM_order, 1)
    % Calculate the CDF of the BER difference
    maxBerMap{1, q} = reshape(max(berMap{1, q}, [], 1), [N_rx, N_ry]);
    berDiff{1, q} = maxBerMap{1, q} - berMapPerfectCase{1, q};
    berRelativeDiff{1, q} = berDiff{1, q} ./ berMapPerfectCase{1, q};

    % Replace NaN or infinite values with 0 (add this block)
    berRelativeDiff{1, q}(isnan(berRelativeDiff{1, q}) | isinf(berRelativeDiff{1, q})) = 0;

    [f_berDiff, x_berDiff] = ecdf(berDiff{1, q}(:));
    cdf90_berDiff(q) = x_berDiff(max(find(f_berDiff < 0.9)));

    [f_berRelativeDiff, x_berRelativeDiff] = ecdf(berRelativeDiff{1, q}(:));
    cdf90_berRelativeDiff(q) = x_berRelativeDiff(max(find(f_berRelativeDiff < 0.9)));

    % Calculate the coverage area for each direction
    coverageMap = zeros(N_rx, N_ry);
    for i_n = 1:N_n_t
        % Extract the coverage for the current direction
        currentCoverage = squeeze(berMap{i_r_t, q}(i_n, :, :)) < 3.8e-3; % Squeeze to get a 2D matrix
        coverageMap = coverageMap + double(currentCoverage); % Add current coverage to total
    end

    % Calculate intersection of coverage areas
    intersectionMap = (coverageMap == N_n_t); % Only points covered in all 4 directions
    coverage(q) = sum(intersectionMap(:)) * (L * W) / (N_rx * N_ry); % Final coverage area
    coverageDiff(q) = coverage(q) - coveragePerfectCase(q);
    coverageRelativeDiff(q) = coverageDiff(q) / coveragePerfectCase(q);

    % Store maps for visualization
    coverageMaps{q} = coverageMap;
    intersectionMaps{q} = intersectionMap;
    intersectionAreas(q) = sum(intersectionMap(:)) * (L * W) / (N_rx * N_ry); % Calculate and store intersection area
end

%% PLOTS

% Plot relative difference in coverage area
figure;
plot(log2(QAM_order(:, 1)'), coverageRelativeDiff .* 100, '-o');
xticks(log2(QAM_order(:, 1)'));
xticklabels(string(QAM_order(:, 1)'));
xlabel('QAM order (M)');
ylabel('Relative difference in coverage area (%)');
grid on;
title('Relative difference in coverage area vs QAM order');

% Plot relative difference in BER (90th percentile)
figure;
plot(log2(QAM_order(:, 1)'), cdf90_berRelativeDiff .* 100, '-o');
xticks(log2(QAM_order(:, 1)'));
xticklabels(string(QAM_order(:, 1)'));
xlabel('QAM order (M)');
ylabel('Relative difference in BER (%)');
grid on;
title('Relative difference in BER (90th percentile) vs QAM order');

% Plot coverage evolution for perfect and misalignment cases
figure;
plot(log2(QAM_order(:, 1)'), coveragePerfectCase, '-o', 'LineWidth', 1.5, 'DisplayName', 'Perfect Case');
hold on;
plot(log2(QAM_order(:, 1)'), intersectionAreas, '-s', 'LineWidth', 1.5, 'DisplayName', 'Misalignment Case');
hold off;

% Customize the x-axis
xticks(log2(QAM_order(:, 1)'));
xticklabels(string(QAM_order(:, 1)'));

% Add labels, title, and legend
xlabel('QAM Order (M)');
ylabel('Coverage Area (m^2)');
title('Coverage Evolution: Perfect vs. Misalignment Cases');
legend('Location', 'northeast');
grid on;

本文标签: How to Load Multiple Files in MATLABProcess Themand Plot ResultsStack Overflow