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 ビュー (過去 30 日間)
Brandy Barker
Brandy Barker 2023 年 6 月 5 日
回答済み: Yatharth 2023 年 8 月 24 日
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 件のコメント
VBBV
VBBV 2023 年 6 月 6 日
編集済み: VBBV 2023 年 6 月 6 日
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.
Brandy Barker
Brandy Barker 2023 年 6 月 6 日
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).

サインインしてコメントする。

回答 (1 件)

Yatharth
Yatharth 2023 年 8 月 24 日
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.

カテゴリ

Help Center および File ExchangeDevelop Apps Using App Designer についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by