MATLAB Plotting: xticks Error and Legend Repetition Issue?
I’m working on a MATLAB project where I need to analyze and plot the relative difference in coverage area for different QAM orders and semi-angles (ϕ_1/2) for a wireless communication simulation. I’ve encountered two persistent errors:
Error using xticks (line 33)
Value must be a numeric vector whose values increase.
Error in untitled11 (line 84)
xticks(QAM_order); % Ensure QAM_order is numeric and sorted
and One more thing is the legend has multiples 15, 30, 45 as shown in the below image
Expected Outcome:
Correct x-axis ticks: The x-axis should display the QAM orders {16,32,64,128,256,512,1024}.
Unique legend entries: Each angle (ϕ1/2 =15 ∘ ,30 ∘ ,45 ∘) should appear exactly once in the legend.
Any assistance, please?
here is below my code:
clear variables;
close all;
clc;
%% Define angles and file paths
angles = [15, 30, 45]; % Define angles
angleLabels = ["15°", "30°", "45°"]; % Labels for legends
numAngles = length(angles);
% Load data from the first angle to initialize QAM_order
load([‘workspace_withBeamsteering_’, num2str(angles(1)), ‘.mat’]); % Replace with actual file path
QAM_order = unique(sort(double(QAM_order(:)))); % Ensure QAM_order is numeric, sorted, and unique
numQAMOrders = length(QAM_order); % Dynamically determine number of QAM orders
% Debug: Display QAM_order
disp(‘QAM_order:’);
disp(QAM_order);
% Initialize storage for results
coverageRelativeDiffAll = zeros(numAngles, numQAMOrders); % Initialize storage for relative differences
%% Process data for each angle
for a = 1:numAngles
angle = angles(a);
% Load data for the perfect beamsteering case
load([‘workspace_withBeamsteering_’, num2str(angle), ‘.mat’]); % Replace with actual file path
berMapPerfectCase = berMap;
coveragePerfectCase = coverage;
% Load data for the beamsteering with misalignment case
load([‘workspace_’, num2str(angle), ‘_withBeamsteering_Misalignment.mat’]); % Replace with actual file path
% Compute relative difference in coverage area for this angle
for q = 1:numQAMOrders
% Ensure correct size for coverageMap
[N_directions, N_rx, N_ry] = size(berMap{q});
coverageMap = zeros(N_rx, N_ry);
% Accumulate coverage for all directions
for i_n = 1:N_directions
currentCoverage = squeeze(berMap{q}(i_n, :, :)) < 3.8e-3;
coverageMap = coverageMap + double(currentCoverage);
end
% Compute the intersection map (points covered in all directions)
intersectionMap = (coverageMap == N_directions);
% Calculate misalignment coverage
coverageMisalignment = sum(intersectionMap(:)) * (L * W) / numel(intersectionMap);
% Compute relative difference
coverageRelativeDiffAll(a, q) = (coverageMisalignment – coveragePerfectCase(q)) / coveragePerfectCase(q);
end
end
%% Plot Relative Difference in Coverage Area for All Angles
figure;
hold on;
% Define markers and colors for each angle
markers = {‘-o’, ‘-s’, ‘-^’}; % Different markers for angles
colors = {‘b’, ‘r’, ‘g’}; % Blue, Red, Green
for a = 1:numAngles
plot(QAM_order, coverageRelativeDiffAll(a, 🙂 * 100, …
markers{a}, ‘LineWidth’, 1.5, ‘Color’, colors{a}, …
‘DisplayName’, [‘$phi_{1/2} = $ ‘, num2str(angles(a)), ‘$^circ$’]);
end
hold off;
% Add labels, title, and legend
xlabel(‘QAM Order (M)’, ‘FontSize’, 12, ‘FontWeight’, ‘bold’);
ylabel(‘Relative Difference in Coverage Area (%)’, ‘FontSize’, 12, ‘FontWeight’, ‘bold’);
title(‘Relative Difference in Coverage Area vs QAM Order’, ‘FontSize’, 12, ‘FontWeight’, ‘bold’);
legend(‘Location’, ‘northeast’, ‘FontSize’, 11, ‘Interpreter’, ‘latex’);
% Set grid and font appearance
grid on;
set(gca, ‘FontSize’, 11, ‘FontWeight’, ‘bold’);
% Set x-axis to show QAM order directly
xticks(QAM_order); % Ensure QAM_order is numeric and sorted
xticklabels(string(QAM_order)); % Display QAM order values directly
% Set x-axis limits to start at the smallest QAM order
xlim([min(QAM_order), max(QAM_order)]); % Start from the minimum and go to the maximum QAM orderI’m working on a MATLAB project where I need to analyze and plot the relative difference in coverage area for different QAM orders and semi-angles (ϕ_1/2) for a wireless communication simulation. I’ve encountered two persistent errors:
Error using xticks (line 33)
Value must be a numeric vector whose values increase.
Error in untitled11 (line 84)
xticks(QAM_order); % Ensure QAM_order is numeric and sorted
and One more thing is the legend has multiples 15, 30, 45 as shown in the below image
Expected Outcome:
Correct x-axis ticks: The x-axis should display the QAM orders {16,32,64,128,256,512,1024}.
Unique legend entries: Each angle (ϕ1/2 =15 ∘ ,30 ∘ ,45 ∘) should appear exactly once in the legend.
Any assistance, please?
here is below my code:
clear variables;
close all;
clc;
%% Define angles and file paths
angles = [15, 30, 45]; % Define angles
angleLabels = ["15°", "30°", "45°"]; % Labels for legends
numAngles = length(angles);
% Load data from the first angle to initialize QAM_order
load([‘workspace_withBeamsteering_’, num2str(angles(1)), ‘.mat’]); % Replace with actual file path
QAM_order = unique(sort(double(QAM_order(:)))); % Ensure QAM_order is numeric, sorted, and unique
numQAMOrders = length(QAM_order); % Dynamically determine number of QAM orders
% Debug: Display QAM_order
disp(‘QAM_order:’);
disp(QAM_order);
% Initialize storage for results
coverageRelativeDiffAll = zeros(numAngles, numQAMOrders); % Initialize storage for relative differences
%% Process data for each angle
for a = 1:numAngles
angle = angles(a);
% Load data for the perfect beamsteering case
load([‘workspace_withBeamsteering_’, num2str(angle), ‘.mat’]); % Replace with actual file path
berMapPerfectCase = berMap;
coveragePerfectCase = coverage;
% Load data for the beamsteering with misalignment case
load([‘workspace_’, num2str(angle), ‘_withBeamsteering_Misalignment.mat’]); % Replace with actual file path
% Compute relative difference in coverage area for this angle
for q = 1:numQAMOrders
% Ensure correct size for coverageMap
[N_directions, N_rx, N_ry] = size(berMap{q});
coverageMap = zeros(N_rx, N_ry);
% Accumulate coverage for all directions
for i_n = 1:N_directions
currentCoverage = squeeze(berMap{q}(i_n, :, :)) < 3.8e-3;
coverageMap = coverageMap + double(currentCoverage);
end
% Compute the intersection map (points covered in all directions)
intersectionMap = (coverageMap == N_directions);
% Calculate misalignment coverage
coverageMisalignment = sum(intersectionMap(:)) * (L * W) / numel(intersectionMap);
% Compute relative difference
coverageRelativeDiffAll(a, q) = (coverageMisalignment – coveragePerfectCase(q)) / coveragePerfectCase(q);
end
end
%% Plot Relative Difference in Coverage Area for All Angles
figure;
hold on;
% Define markers and colors for each angle
markers = {‘-o’, ‘-s’, ‘-^’}; % Different markers for angles
colors = {‘b’, ‘r’, ‘g’}; % Blue, Red, Green
for a = 1:numAngles
plot(QAM_order, coverageRelativeDiffAll(a, 🙂 * 100, …
markers{a}, ‘LineWidth’, 1.5, ‘Color’, colors{a}, …
‘DisplayName’, [‘$phi_{1/2} = $ ‘, num2str(angles(a)), ‘$^circ$’]);
end
hold off;
% Add labels, title, and legend
xlabel(‘QAM Order (M)’, ‘FontSize’, 12, ‘FontWeight’, ‘bold’);
ylabel(‘Relative Difference in Coverage Area (%)’, ‘FontSize’, 12, ‘FontWeight’, ‘bold’);
title(‘Relative Difference in Coverage Area vs QAM Order’, ‘FontSize’, 12, ‘FontWeight’, ‘bold’);
legend(‘Location’, ‘northeast’, ‘FontSize’, 11, ‘Interpreter’, ‘latex’);
% Set grid and font appearance
grid on;
set(gca, ‘FontSize’, 11, ‘FontWeight’, ‘bold’);
% Set x-axis to show QAM order directly
xticks(QAM_order); % Ensure QAM_order is numeric and sorted
xticklabels(string(QAM_order)); % Display QAM order values directly
% Set x-axis limits to start at the smallest QAM order
xlim([min(QAM_order), max(QAM_order)]); % Start from the minimum and go to the maximum QAM order I’m working on a MATLAB project where I need to analyze and plot the relative difference in coverage area for different QAM orders and semi-angles (ϕ_1/2) for a wireless communication simulation. I’ve encountered two persistent errors:
Error using xticks (line 33)
Value must be a numeric vector whose values increase.
Error in untitled11 (line 84)
xticks(QAM_order); % Ensure QAM_order is numeric and sorted
and One more thing is the legend has multiples 15, 30, 45 as shown in the below image
Expected Outcome:
Correct x-axis ticks: The x-axis should display the QAM orders {16,32,64,128,256,512,1024}.
Unique legend entries: Each angle (ϕ1/2 =15 ∘ ,30 ∘ ,45 ∘) should appear exactly once in the legend.
Any assistance, please?
here is below my code:
clear variables;
close all;
clc;
%% Define angles and file paths
angles = [15, 30, 45]; % Define angles
angleLabels = ["15°", "30°", "45°"]; % Labels for legends
numAngles = length(angles);
% Load data from the first angle to initialize QAM_order
load([‘workspace_withBeamsteering_’, num2str(angles(1)), ‘.mat’]); % Replace with actual file path
QAM_order = unique(sort(double(QAM_order(:)))); % Ensure QAM_order is numeric, sorted, and unique
numQAMOrders = length(QAM_order); % Dynamically determine number of QAM orders
% Debug: Display QAM_order
disp(‘QAM_order:’);
disp(QAM_order);
% Initialize storage for results
coverageRelativeDiffAll = zeros(numAngles, numQAMOrders); % Initialize storage for relative differences
%% Process data for each angle
for a = 1:numAngles
angle = angles(a);
% Load data for the perfect beamsteering case
load([‘workspace_withBeamsteering_’, num2str(angle), ‘.mat’]); % Replace with actual file path
berMapPerfectCase = berMap;
coveragePerfectCase = coverage;
% Load data for the beamsteering with misalignment case
load([‘workspace_’, num2str(angle), ‘_withBeamsteering_Misalignment.mat’]); % Replace with actual file path
% Compute relative difference in coverage area for this angle
for q = 1:numQAMOrders
% Ensure correct size for coverageMap
[N_directions, N_rx, N_ry] = size(berMap{q});
coverageMap = zeros(N_rx, N_ry);
% Accumulate coverage for all directions
for i_n = 1:N_directions
currentCoverage = squeeze(berMap{q}(i_n, :, :)) < 3.8e-3;
coverageMap = coverageMap + double(currentCoverage);
end
% Compute the intersection map (points covered in all directions)
intersectionMap = (coverageMap == N_directions);
% Calculate misalignment coverage
coverageMisalignment = sum(intersectionMap(:)) * (L * W) / numel(intersectionMap);
% Compute relative difference
coverageRelativeDiffAll(a, q) = (coverageMisalignment – coveragePerfectCase(q)) / coveragePerfectCase(q);
end
end
%% Plot Relative Difference in Coverage Area for All Angles
figure;
hold on;
% Define markers and colors for each angle
markers = {‘-o’, ‘-s’, ‘-^’}; % Different markers for angles
colors = {‘b’, ‘r’, ‘g’}; % Blue, Red, Green
for a = 1:numAngles
plot(QAM_order, coverageRelativeDiffAll(a, 🙂 * 100, …
markers{a}, ‘LineWidth’, 1.5, ‘Color’, colors{a}, …
‘DisplayName’, [‘$phi_{1/2} = $ ‘, num2str(angles(a)), ‘$^circ$’]);
end
hold off;
% Add labels, title, and legend
xlabel(‘QAM Order (M)’, ‘FontSize’, 12, ‘FontWeight’, ‘bold’);
ylabel(‘Relative Difference in Coverage Area (%)’, ‘FontSize’, 12, ‘FontWeight’, ‘bold’);
title(‘Relative Difference in Coverage Area vs QAM Order’, ‘FontSize’, 12, ‘FontWeight’, ‘bold’);
legend(‘Location’, ‘northeast’, ‘FontSize’, 11, ‘Interpreter’, ‘latex’);
% Set grid and font appearance
grid on;
set(gca, ‘FontSize’, 11, ‘FontWeight’, ‘bold’);
% Set x-axis to show QAM order directly
xticks(QAM_order); % Ensure QAM_order is numeric and sorted
xticklabels(string(QAM_order)); % Display QAM order values directly
% Set x-axis limits to start at the smallest QAM order
xlim([min(QAM_order), max(QAM_order)]); % Start from the minimum and go to the maximum QAM order plot, xticks, legend MATLAB Answers — New Questions