Can you check whether the ModelButtonDown and ModelPaneButtonDown components are grouped in the design view ?
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.
6 views (last 30 days)
Show older comments
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
VBBV
on 6 Jun 2023
Edited: VBBV
on 6 Jun 2023
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.
Answers (1)
Yatharth
on 24 Aug 2023
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:
- Open App Designer in MATLAB.
- Add a UIAxes component to your app's user interface. This will be the plot where you want to capture the click coordinates.
- 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.
- 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
0 Comments
See Also
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!

