My AppDesigner (R2022a) has a model on the plot; I need to be able to click *anywhere* in the model/plot and store the coordinates of the click.

My app automatically inports the model and adds it to the plot, along with the default boundary lines to plot. I need to be able to click on the plot (or model) and store the coordinates of that click, later those coordinates will be used to update the boundary lines.
function startupFcn(app)
load('TIMagneticModels.ti','bounds','data','model','topo','-mat')
app.magData.bounds=bounds;
app.magData.data=data;
app.magData.model=model;
app.magData.topo=topo;
app.Sb.Value=round(min(app.magData.bounds.ycut)-500);
app.Nb.Value=round(max(bounds.ycut)+500);
app.Eb.Value=round(max(bounds.xcut)+500);
app.Wb.Value=round(min(bounds.xcut)-500);
UpdateBounds(app);
GridSize(app);
plotTMIBounds(app);
end
function ModelButtonDown(app, event) % Button Down function of app.Model
clickedPoint = get(app.Model, 'CurrentPoint');
xCoord = clickedPoint(1,1)
yCoord = clickedPoint(1,2)
app.Nb.Value = xCoord
app.Wb.Value = yCoord
end
function ModelPaneButtonDown(app, event) % Button Down function of app.ModelPane
clickedPoint = get(app.Model, 'CurrentPoint');
xCoord = clickedPoint(1,1)
yCoord = clickedPoint(1,2)
app.Nb.Value = xCoord
app.Wb.Value = yCoord
end
This code works only to store the coordinates if they are not on the model. Clicking on the model saves nothing.

4 Comments

Can you check whether the ModelButtonDown and ModelPaneButtonDown components are grouped in the design view ?

Do you mean this?
Or that when I select app.ModelPane it also automatically shows the edges of app.Model?
Yes, the latter one. It seems they are both grouped together. Can you ungroup them and try again whether it records the coordinates of the clicked point on the model now ? It also appears (from former snapshot) that model is child of modelpane component.
I wasn't sure how to ungroup them, so I just deleted the panel. It still reocgnizes and stores the coordinates when I click in the white space above/below the model, but not on the model itself (red box).

Sign in to comment.

Answers (1)

Hi,
I understand that you are trying to store the x and y coordinate when clicking on the plot inside the App Designer
To implement the functionality of clicking on the plot or model in App Designer and storing the coordinates of the click, you can follow these steps:
  1. Open App Designer in MATLAB.
  2. Add a UIAxes component to your app's user interface. This will be the plot where you want to capture the click coordinates.
  3. In the "Properties" section of the UIAxes component, set the 'ButtonDownFcn' property to UIAxesButtonDown. This property allows you to specify the callback function that will be triggered when a mouse button is clicked on the plot.
  4. In the "Code View" section of App Designer, add the following code:
methods (Access = private)
% Button down function: UIAxes
function UIAxesButtonDown(app, event)
clickCoords = get(app.UIAxes, 'CurrentPoint');
x = clickCoords(1,1);
y = clickCoords(1,2);
% Store the click coordinates in app variables
app.clickedX = x;
app.clickedY = y;
% Call a function to update the boundary lines using the stored coordinates
updateBoundaryLines(app);
end
end
5. Make sure to define clickedX and clickedY explicitly in app's class definition to access them
properties (Access = private)
clickedX double
clickedY double
end
6. Now you can define your updateBoundaryLines function which is being called inside UIAxesButtonDown function
methods (Access = private)
function updateBoundaryLines(app)
coordinatesText = sprintf('Clicked at (%.2f, %.2f)', app.clickedX , app.clickedY);
disp(coordinatesText);
end
end
you can learn more about UIAxes from this link.

Categories

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

Products

Release

R2022a

Asked:

on 5 Jun 2023

Answered:

on 24 Aug 2023

Community Treasure Hunt

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

Start Hunting!