MATLAB Answers

extracting points from trisurf in a GUI

4 views (last 30 days)
Jason Riley
Jason Riley on 3 May 2017
Answered: Chaitral Date on 8 May 2017
So I started a GUI using GUIDE. i want to be able to click on a point in a figure created by trisurf in axes1.
i added a 'button down' function using the GUIDE interface. but when i add code there as follows:
get(hObject,'CurrentPoint')
set(handles.text4,'string', num2str(x));
guidata(hObject, handles);
then i have 2 issues: (1) if the trisurf hasn't been initialised via button click i get the error ??? Input argument "handles" is undefined.
Error in ==> GUI>axes1_ButtonDownFcn at 228 set(handles.text4,'string', num2str(x(1,1)));
??? Error while evaluating axes ButtonDownFcn
(2) once the trisurf function is initiated (via a pushbutton), the buttondownfcn no longer works at all
So, help? not even sure the CurrentPoint is the right comand, but the whole thing is a bit hinky.
p.s. sorry i have used matlab for ages, just never done GUI's - i'm working with users who can't handle doing things manually...

Answers (1)

Chaitral Date
Chaitral Date on 8 May 2017
Use the below code,
hobj = figure;
handles=guihandles(hobj); %Create structure of handles
[x,y] = meshgrid(1:15,1:15);
tri = delaunay(x,y);
z = peaks(15);
P1=trisurf(tri,x,y,z);
Ax=gca;
handles.P1=P1;
handles.Ax=Ax;
hobj.WindowButtonDownFcn=@getPoint;
guidata(hobj,handles); %save the structure
function getPoint(hobj,eventdata)
handles = guidata(hobj); %returns previously stored data
Ax=handles.Ax;
Ax.CurrentPoint
end
This will work without any error. You have to use CurrentPoint property of Axes. Although, I am not sure what you are trying to achieve in the below line,
>> set(handles.text4,'string', num2str(x));

Community Treasure Hunt

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

Start Hunting!