Hi Gianmarco,
I understand that you want to annotate data points in a "scatter" plot in MATLAB, with text chosen from dropdown menu options, which appears on clicking the datapoints.
Here is a solution to achieve the above use case using nested callback functions –
- Enable ‘data cursor’ mode using the “datacursormode” function, which allows interactive labelling of points by clicking on them.
- Create a callback function named “showDropDown” and assign it to the “UpdateFcn” property of the “datacursormode” object.
- Inside the “showDropDown” function, use the “uicontrol” function to create a dropdown menu.
- Pass a function called assignVariableName as the callback to assign the selected text (annotation) to the data points.
For your better understanding, I am attaching the code snippet that demonstrates this approach:
set(dcm, 'Enable', 'on');
dcm.UpdateFcn = @showDropdown;
function output_txt = showDropdown(~, event_obj)
xClicked = event_obj.Position(1);
yClicked = event_obj.Position(2);
variableNames = {'A', 'B', 'C', 'D'};
dropdown = uicontrol('Style', 'popupmenu', 'String', variableNames, 'Position', [50, 40, 100, 20], 'Callback', @assignVariableName);
function assignVariableName(source, ~)
selectedVariable = variableNames{get(source, 'Value')};
text(xClicked, yClicked, selectedVariable, 'Color', 'red',FontSize=50);
Further, you can refer to the following documentations of the functions used in the code snippet for more details:
- datacursormode: https://www.mathworks.com/help/matlab/ref/matlab.graphics.shape.internal.datacursormanager.html
- uicontrol: https://in.mathworks.com/help/matlab/ref/uicontrol.html
I hope you find the provided solution useful and this solution helps you label your data points according to your desired workflow.
Regards,
Vinayak Luha