why i can't delete a surface 3d plot (fsurf i mean) whereas i can delete a plot. If it can be, how can i do that

4 views (last 30 days)
So i made some checkbox, one of them is surface checkbox .Then, when i turn it on the surface is come up and when it off the surface is supposed to be disappear. But i got error instead. How can i solve this problem ???
function surface_Callback(hObject, eventdata, handles)
% hObject handle to surface (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of surface
% plot3(x1,x2,ynonlinereg,'or','LineWidth',4);
handles = guidata(hObject);
x1 = handles.x1;
x2 = handles.x2;
x = handles.x;
if get(handles.surface,'Value') == 1
g=@(x1,x2) x(1,1) + x(2,1).*x1 + x(3,1).*x2 + x(4,1).*(x1.^2) + x(5,1).*(x2.^2) + x(6,1).*(x1.*x2);
fsurf(g);
grid on;
xlim([70 90]);
ylim([0 5]);
zlim([-0.04 0.06]);
else
delete(fsurf); % I thought it was success
end

Answers (2)

dpb
dpb on 11 Jun 2022
fsurf(g);
does the plot but doesn't return the handle by which to reference it for the delete() operation.
hFS=fsurf(g); % draw surface; save handle
Then use
delete(hFS)

Voss
Voss on 12 Jun 2022
In order for the surface to be accessible in function calls subsequent to the one in which it is created (e.g., to delete the surface later), it is convenient to store the surface's handle in the handles structure.
In the Opening function, you can make a field in the handles structure for the surface's handle, which is initially empty:
handles.my_surface = [];
guidata(hObject,handles); % store the handles structure if you aren't already doing so
Then in the checkbox's Callback function, delete the old and/or create a new surface as appropriate:
function surface_Callback(hObject, eventdata, handles)
% plot3(x1,x2,ynonlinereg,'or','LineWidth',4);
handles = guidata(hObject);
% Regardless of whether or not the checkbox is checked, go ahead and delete
% the surface (if any) here. If the checkbox is checked, a new surface will
% be created soon; otherwise, no new surface will be created. In either
% case, the old surface needs to be deleted.
% (Note that if handles.my_surface is empty, this is "delete([])", which
% has no effect.)
delete(handles.my_surface);
% Also replace the deleted surface with the empty placeholder in handles
% structure (you don't want a handle to a deleted object hanging around).
% (This could also be done in an "else" block below, since
% handles.my_surface will be set in the "if" block.)
handles.my_surface = [];
if get(handles.surface,'Value')
x1 = handles.x1;
x2 = handles.x2;
x = handles.x;
g=@(x1,x2) x(1,1) + x(2,1).*x1 + x(3,1).*x2 + x(4,1).*(x1.^2) + x(5,1).*(x2.^2) + x(6,1).*(x1.*x2);
% create the surface and store it in the handles structure
handles.my_surface = fsurf(g);
grid on;
xlim([70 90]);
ylim([0 5]);
zlim([-0.04 0.06]);
% else % (As mentioned above, you could do it this
% handles.my_surface = []; % way too.)
end
% IMPORTANT: store the updated handles structure
guidata(hObject,handles);

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!