Clear Filters
Clear Filters

highlighting a datapoint in a plot of data specific to a state using a checkbox

9 views (last 30 days)
Hi all,
I am very new to matlab, and am trying to add a component to an app I'm making. The app plots an x,y set of data from an excel spread sheet. each point is specific to a N.A. state. I want to add a check box for each state that when checked, will circle the data point asssociated to that state without getting rid of the rest of the points on my plot. I could also use a dropdown if its easier.
Thanks for your time,
Liam

Answers (1)

Suraj Kumar
Suraj Kumar on 21 Mar 2024
Hi Liam,
To highlight a point on a plot that is selected from a dropdown menu , you can refer to the following code snippet.
To add a dropdown menu for the data extracted from an excel file:
function LoadButtonPushed(app, event)
[file, path] = uigetfile('data.xlsx', 'Select an Excel file');
if isequal(file, 0)
return;
end
fullPath = fullfile(path, file);
data = readmatrix(fullPath);
app.xData = data(:, 1);
app.yData = data(:, 2);
% Plot the data
plot(app.UIAxes, app.xData, app.yData, 'b-');
numPoints = length(app.xData);
dropdownItems = arrayfun(@(i) sprintf('Point %d', i), 1:numPoints, 'UniformOutput', false);
app.DropDown.Items = dropdownItems;
end
This code opens a file selection dialog, reads data from the selected Excel file, plots the data, updates the dropdown menu with point identifiers.
Now, after populating the dropdown menu with data points, highlight the point whenever it is selected from the menu by adding a specific marker on the data point in the plot.
function PointSelectionDropDownValueChanged(app, event)
pointIndex = str2double(regexp(event.Value, '\d+', 'match', 'once'));
if ~isempty(app.HighlightedPoint) && isvalid(app.HighlightedPoint)
delete(app.HighlightedPoint);
end
% Highlight the selected point
hold(app.UIAxes, 'on');
app.HighlightedPoint = plot(app.UIAxes, app.xData(pointIndex), app.yData(pointIndex), 'ro', 'MarkerSize', 10);
hold(app.UIAxes, 'off');
end
You might find these resources helpful:
Hope this helps!

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!