Search code examples
matlabundefinedmatlab-figurematlab-guideaxes

how can i set current axes in user defined function in MATLAB?


I am writing a program with gui . except functions that created automatic such as callbacks for objects in gui , i want to create user defined functions to use them in everywhere in this program . so i created meshing function that i write below .

function meshing(p1, p2, p3, p4, p5, p6, p7, p8)

pmb = [( ( p1(1) + p5(1) ) / 2 ) ( ( p1(2) + p4(2) ) / 2 ) p1(3) ];
pmt = [( ( p1(1) + p5(1) ) / 2 ) ( ( p1(2) + p4(2) ) / 2 ) p2(3) ];

p23 = ( p2 + p3 ) / 2;

a = get(gcf);
axes(handles.axes3d);
scatter3(pmt(1),pmt(2),pmt(3))

but when i run it , this below error is showing

Error while evaluating UIControl Callback

Undefined variable "handles" or class "handles.axes3d".

Error in SSF>meshing (line 897) axes(handles.axes3d);

but i have a axes3d in my gui . what can i do


Solution

  • The primary issue is that handles is not defined within your function because it is not passed as an input. If you look at all internally-defined callbacks, they have handles as an input.

    Without seeing the exact code for how you're having your GUI call this meshing function, you could get the guidata of the GUI to have access to the handles variable from within your function.

    handles = guidata(gcf);
    

    Then you can use it within your function

    scatter3(pmt(1), pmt(2), pmt(3), 'Parent', handles.axes3d)