How can i make the output hide and show only when the output when the cursor is placed

2 次查看(过去 30 天)
This is the code i am working it but i have a problem when i run the output it gives output on top of each point but i want it to be hidden and displayed only when i click that point,how can i make it that way.if you look at the output it will explain much clearer (Too much text)
% Load your data
data = readmatrix('subtowersutm.xlsx');
% Extract latitude, longitude, and RSRP values.
measurement_lat = data(:, 1);
measurement_lon = data(:, 2);
rsrp = data(:, 11); % Adjust the column index for RSRP data.
% Check if the dimensions of Lat and Lon match
if size(measurement_lat) ~= size(measurement_lon)
error('Latitude and Longitude dimensions do not match.');
end
% Define the UTM zone for your area.
utm_zone = 32;
% Step 2: Transform Coordinates using the built-in 'deg2utm' function
[utm_x, utm_y, utm_zone] = deg2utm(measurement_lat, measurement_lon);
% Define the pixel size and create the grid
pixel_size = 10; % Adjust as needed
x_grid = min(utm_x):pixel_size:max(utm_x);
y_grid = min(utm_y):pixel_size:max(utm_y);
% Step 3: Calculate average RSRP for each pixel
num_pixels_x = numel(x_grid) - 1;
num_pixels_y = numel(y_grid) - 1;
average_rsrp = zeros(num_pixels_y, num_pixels_x); % Initialize the grid.
% Initialize a matrix to store the count of values considered
value_count = zeros(num_pixels_y, num_pixels_x);
for i = 1:num_pixels_x
for j = 1:num_pixels_y
% Define the current pixel polygon.
polygon_x = [x_grid(i), x_grid(i + 1), x_grid(i + 1), x_grid(i)];
polygon_y = [y_grid(j), y_grid(j), y_grid(j + 1), y_grid(j + 1)];
% Check if measurements fall within the current pixel.
in_polygon = inpolygon(utm_x, utm_y, polygon_x, polygon_y);
% Calculate average RSRP for the measurements within the polygon.
if any(in_polygon)
rsrp_values_in_polygon = rsrp(in_polygon); % Replace with your RSRP data.
% Convert dBm to watts, compute average, and convert back to dBm.
rsrp_watts = 10 .^ (rsrp_values_in_polygon / 10);
average_rsrp(j, i) = 10 * log10(mean(rsrp_watts));
value_count(j, i) = numel(rsrp_values_in_polygon); % Store the count of values
end
end
end
% Exclude cells with 0 value from the heatmap
average_rsrp(average_rsrp == 0) = NaN;
% Define the color for NaN values (white)
nanColor = [1, 1, 1]; % RGB color for white
% Create a new figure
fig = figure;
% Plot the heatmap of average RSRP with NaN values represented as white
imagesc(x_grid(1:end-1), y_grid(1:end-1), average_rsrp, 'AlphaData', ~isnan(average_rsrp));
colormap(parula);
colorbar;
xlabel('UTM X');
ylabel('UTM Y');
title('Average RSRP Heatmap (Excluding 0 Values) with Value Count');
% Add text annotations for value count in each plot
for i = 1:num_pixels_x
for j = 1:num_pixels_y
if ~isnan(average_rsrp(j, i))
text(x_grid(i), y_grid(j), sprintf('Count: %d', value_count(j, i)), ...
'HorizontalAlignment', 'center', 'VerticalAlignment', 'middle', 'Color', 'k');
end
end
end

采纳的回答

Voss
Voss 2023-10-7
This will toggle the visibility of a text object when you click on its corresponding pixel in the image. All text objects are initially invisible.
% Load your data
data = readmatrix('subtowersutm.xlsx');
% Extract latitude, longitude, and RSRP values.
measurement_lat = data(:, 1);
measurement_lon = data(:, 2);
rsrp = data(:, 11); % Adjust the column index for RSRP data.
% Check if the dimensions of Lat and Lon match
if size(measurement_lat) ~= size(measurement_lon)
error('Latitude and Longitude dimensions do not match.');
end
% Define the UTM zone for your area.
utm_zone = 32;
% Step 2: Transform Coordinates using the built-in 'deg2utm' function
[utm_x, utm_y, utm_zone] = deg2utm(measurement_lat, measurement_lon);
% Define the pixel size and create the grid
pixel_size = 10; % Adjust as needed
x_grid = min(utm_x):pixel_size:max(utm_x);
y_grid = min(utm_y):pixel_size:max(utm_y);
% Step 3: Calculate average RSRP for each pixel
num_pixels_x = numel(x_grid) - 1;
num_pixels_y = numel(y_grid) - 1;
average_rsrp = zeros(num_pixels_y, num_pixels_x); % Initialize the grid.
% Initialize a matrix to store the count of values considered
value_count = zeros(num_pixels_y, num_pixels_x);
for i = 1:num_pixels_x
for j = 1:num_pixels_y
% Define the current pixel polygon.
polygon_x = [x_grid(i), x_grid(i + 1), x_grid(i + 1), x_grid(i)];
polygon_y = [y_grid(j), y_grid(j), y_grid(j + 1), y_grid(j + 1)];
% Check if measurements fall within the current pixel.
in_polygon = inpolygon(utm_x, utm_y, polygon_x, polygon_y);
% Calculate average RSRP for the measurements within the polygon.
if any(in_polygon)
rsrp_values_in_polygon = rsrp(in_polygon); % Replace with your RSRP data.
% Convert dBm to watts, compute average, and convert back to dBm.
rsrp_watts = 10 .^ (rsrp_values_in_polygon / 10);
average_rsrp(j, i) = 10 * log10(mean(rsrp_watts));
value_count(j, i) = numel(rsrp_values_in_polygon); % Store the count of values
end
end
end
% Exclude cells with 0 value from the heatmap
average_rsrp(average_rsrp == 0) = NaN;
% Define the color for NaN values (white)
nanColor = [1, 1, 1]; % RGB color for white
% Create a new figure
fig = figure;
% Plot the heatmap of average RSRP with NaN values represented as white
img = imagesc(x_grid(1:end-1), y_grid(1:end-1), average_rsrp, 'AlphaData', ~isnan(average_rsrp));
colormap(parula);
colorbar;
xlabel('UTM X');
ylabel('UTM Y');
title('Average RSRP Heatmap (Excluding 0 Values) with Value Count');
% Add text annotations for value count in each plot
t = gobjects(num_pixels_y,num_pixels_x);
for i = 1:num_pixels_x
for j = 1:num_pixels_y
if ~isnan(average_rsrp(j, i))
t(j,i) = text(x_grid(i), y_grid(j), sprintf('Count: %d', value_count(j, i)), ...
'HorizontalAlignment', 'center', 'VerticalAlignment', 'middle', 'Color', 'k', ...
'Visible', 'off', 'HitTest', 'off', 'PickableParts', 'none');
end
end
end
set(img,'ButtonDownFcn',{@bdf_image,t});
function bdf_image(src,evt,t)
[~,xidx] = min(abs(get(src,'XData')-evt.IntersectionPoint(1)));
[~,yidx] = min(abs(get(src,'YData')-evt.IntersectionPoint(2)));
if ishandle(t(yidx,xidx))
if strcmp(t(yidx,xidx).Visible,'off')
t(yidx,xidx).Visible = 'on';
else
t(yidx,xidx).Visible = 'off';
end
end
end

更多回答(2 个)

Mann Baidi
Mann Baidi 2023-10-7
Hi Sangesh,
I understand you would like to display the text only when the user click on that specific point,for this you can use the "WindowButtonDownFcn" callback function.You can refer to the below code for using the callback function.
set(fig, 'WindowButtonDownFcn',@clicker);
function clicker(average_rsrp,x_grid,y_grid,value_count,~)
[i,j]= ginput(1);
text(x_grid(j), y_grid(i), sprintf('Count: %d', value_count(j, i)), ...
'HorizontalAlignment', 'center', 'VerticalAlignment', 'middle', 'Color', 'k');
end
I hooe this will resolve your issue.

Walter Roberson
Walter Roberson 2023-10-7
I suggest that you use datacursormode instead -- which is the program equivalent of clicking to enable data tips.

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by