MATLAB Plotting: xticks Error and Legend Repetition Issue?

2 次查看(过去 30 天)
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
  5 个评论
Haitham AL Satai
Haitham AL Satai 2025-1-12
I am facing a problem even through uploading the file here. Can I send it to you via email?

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2025-1-11
It is not completely correct that the values passed to xticks() must be numeric. You can also pass duration values or datetime values or categorical values to xticks(), provided that the underlying x axes is duration or datetime or categorical.
QAM_order = unique(sort(double(QAM_order(:)))); % Ensure QAM_order is numeric, sorted, and unique
You have ensured that QAM_order is sorted. Note by the way that you could have just used
QAM_order = unique(double(QAM_order(:))); % Ensure QAM_order is numeric, sorted, and unique
as unique() by default returns sorted values.
plot(QAM_order, coverageRelativeDiffAll(a, :) * 100, ...
markers{a}, 'LineWidth', 1.5, 'Color', colors{a}, ...
'DisplayName', ['$\phi_{1/2} = $ ', num2str(angles(a)), '$^\circ$']);
You plot using QAM_order as the first parameter, and you do that as the first plot since your figure(), so no matter what data type QAM_order is, the x axes should be set to the same datatype.
xticks(QAM_order); % Ensure QAM_order is numeric and sorted
We have already established that the axes should be the same datatype as QAM_order, so the datatypes should match.
We are clutching at straws now. We have to hypothesize that you are using a fairly old version of MATLAB with QAM_order being duration value that was automatically converted by plot() but is not being converted by xticks()...
Or we have to hypothesize that no plot() actually took place because one of those load() overwrites numQAMOrders or numAngles.
Or we have to hypothesize that you have your own private xticks() function.
I would recommend putting in a breakpoint at the xticks() call, and examine the size and datatype of QAM_order, and use
which -all xticks
to be sure that you are not getting a third-party xticks() function, and use
get(gca, 'XAxis')
to double-check that you have a numeric axes constructed rather than a duration or categorical axes.

类别

Help CenterFile Exchange 中查找有关 Test and Measurement 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by