MATLAB GUI - Select a point on a plot and run a function with a push button
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
What I want to do is to get which point the user has clicked on a given plot, and then, when he clicks on a push button, a function that receives as args the (X,Y) coordinates of that point will run. If there's no point selected on the plot, a warning msg will show, telling the user to select a point.
Is this possible?
2 件のコメント
Geoff Hayes
2020 年 5 月 5 日
編集済み: Geoff Hayes
2020 年 5 月 5 日
Pedro - do you need the push button? Or could the user just click on a point in the axes/plot and have the code execute immediately (with that point)? Also, are you using App Designer, GUIDE or programmatically creating your GUI?
I'm using GUIDE.
If it's possible to use the push button that way it'd be preferable, because this other function might take a while to run, and if it executes everytime the user presses the plot, it wouldn't be optimal. But, if that's impossible, just clicking on the plot and having the code to execute should be fine too.
採用された回答
Pedro - in the OpeningFcn for your GUI, add a button down callback for your axes:
set(handles.axes1, 'ButtonDownFcn',@axesButtonDownCallback);
The name of the callback function will be axesButtonDownCallback. The callback will look like
function axesButtonDownCallback(hObject, eventdata)
handles.currentPoint = get(hObject,'CurrentPoint');
guidata(hObject, handles);
We get the CurrentPoint property of the axes (the hObject) and save that to a field in the handles structure. We then need to save the updated structure with guidata. This is important - if you don't do this, then the other callbacks won't have access to this fields that we've added to handles. The pushbutton callback will then be
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if ~isfield(handles, 'currentPoint') || isempty(handles.currentPoint)
fprintf('You need to choose a point!\n');
else
currentPoint = handles.currentPoint;
handles.currentPoint = [];
guidata(hObject,handles);
% do something with point
end
Note how we check to see if the 'currentPoint' field exists in the handles structure or if it does exist, whether it is empty. If either is true, then we prompt the user to choose a point (replacing the fprintf with a dialog). If we do have a point, then we extract it from the structure and then reset that field in the structure so that the user will be prompted to choose a new point next time the button is pressed. (Note that again, calling guidata is important.)
10 件のコメント
Pedro Augusto de Castro e Castro
2020 年 5 月 5 日
編集済み: Pedro Augusto de Castro e Castro
2020 年 5 月 5 日
Thank you for your answer!
I have several axes, and I already have a ButtonDownFcn for each one of them, to show a custom data tip when a point is clicked. How'd I do it then?
So when you press a button, which axes does it correspond to? Or does each axes have a button?
Pedro Augusto de Castro e Castro
2020 年 5 月 5 日
編集済み: Pedro Augusto de Castro e Castro
2020 年 5 月 5 日
I have only one button, that will be used for all the axes. But in each axes, I have several plots too. I need to know which axes and which plot the user is clicking.
I was able to do it for one axes, but how do I know which plot is being clicked?
Other thing, if I do this once, it seems to work. But, if I click on another point and then click again on tha push button if does not work (the msg appears 'You need to choose a point!\n').
Well when you save the current point (to the handles object), you would need to save the handle to the axes that it corresponds to.
function axesButtonDownCallback(hObject, eventdata)
handles.currentPoint = get(hObject,'CurrentPoint');
handles.axesForCurrentPoint = hObject;
guidata(hObject, handles);
As for not being able to get this to work again, either you would need to post your code or use the MATLAB debugger to figure out what is going on.
I don't know if this code will help because it needs a data base and other functions to run (and it's in portuguese too). But here is the main GUI function.
I still don't understand how the code will know which plot is being clicked.
Thanks agains for helping me.
If all of your graph_XX_ButtonDownFcn callbacks work, then you could add the following to each (!) one
handles.currentPoint = get(hObject,'CurrentPoint');
handles.axesForCurrentPoint = hObject;
guidata(hObject, handles);
So now you will know which axes the user last pressed the mouse button down on, axesForCurrentPoint, and where, currentPoint. Now add this code
if ~isfield(handles, 'currentPoint') || isempty(handles.currentPoint)
fprintf('You need to choose a point!\n');
else
currentPoint = handles.currentPoint;
axesForCurrentPoint = handles.axesForCurrentPoint;
handles.currentPoint = [];
handles.axesForCurrentPoint = [];
guidata(hObject,handles);
% do something with point
end
to your push button (I'm not sure which one).
Pedro Augusto de Castro e Castro
2020 年 5 月 5 日
編集済み: Pedro Augusto de Castro e Castro
2020 年 5 月 5 日
ok, thanks!
There's a problem though. Instead of showing the value in red (the real value of the point in the curve), the currentPoint is showing another value, in blue (perhaps the first value that I click on the axes, and not the value of the point on the plot).
In this case, it should show (1.3764 , 0.986265).

I guess the code only runs the axes button down fcn once, and then it gets only the first point I clicked. Also, because of this, the code doesn't work when I press the push button again.
Well if you want the same point, you'll somehow need to use the data from the myupdatefcn....which you can probably do by passing the hObject as a parameter to this function (and then getting and updating the handles structure from withih it). Maybe (and this isn't tested)
set(handles.dcm,'UpdateFcn',@(t,e) myupdatefcn(t,e, handles.PerdasResults, hObject));
with
function myupdatefcn(t,e,results, hObject)
handles = guidata(hObject);
handles.currentPoint = ;% whatever
handles.axesForCurrentPoint = hObject;
guidata(hObject, handles);
% rest of your code
end
As for the code doesn't work when I press the push button again. without seeing your code I won't be able to tell you what is going wrong. Again, try using the debugger to figure it out.
Thanks for your help man! Really helpful!
Just another quick question: how do I debbug, if I'm using a GUI that depends on the user inputs?
no problem! For debugging, just put breakpoints at the lines of code that you are interested in.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Interactive Control and Callbacks についてさらに検索
参考
2020 年 5 月 4 日
2020 年 5 月 5 日
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
