Remove UI Control edit box referenced with handles

Hi,
I have created an UI control edit boxes and are created and stored in handles for eg
for i=1:20
handles.sensname(i) = uicontrol('style', 'edit','Position', [95, b-(i-1)*20, 180, 17],'BackgroundColor',[1 1 1]);
end
Now after creating this, i want to delete the ui control editboxes. What i tried is
for i=1:20
delete(handles.sensname(i));
end
But I am getting the error 'Root object may not be deleted'. i know it may be that handle values cannot be deleted, then how to solve my problem?
Appreciate any idea to solve this
Karthik

 Accepted Answer

Is it possible that your code is not quite as shown, and that you initialize handles.sensname as an array with more elements than you store handles for? If so then the extra entries would be 0 and you would get the error you get.
Suggested replacement code (no loop needed)
delete( handles.sensname(handles.sensname > 0) );

5 Comments

no that also not woking.
I have added the code below, which comes under a pushbutton,
if str2num(get(handles.nosensorentry_tag,'string'))<26
for i=1:str2num(get(handles.nosensorentry_tag,'string'))
handles.sensname(i) = uicontrol('style', 'edit','Position', [95, b-(i-1)*20, 180, 17],'BackgroundColor',[1 1 1]);
handles.sensnot(i) = uicontrol('style', 'edit','Position', [410, b-(i-1)*20, 45, 17],'BackgroundColor',[1 1 1]);
handles.sensvtsno(i) = uicontrol('style', 'edit','Position', [535, b-(i-1)*20, 40, 17],'BackgroundColor',[1 1 1],'string',i);
handles.sensmeasno(i) = uicontrol('style', 'edit','Position', [650, b-(i-1)*20, 40, 17],'BackgroundColor',[1 1 1]);
end
handles.count=str2num(get(handles.nosensorentry_tag,'string'));
guidata(hObject, handles);
else
for i=1:handles.count
delete(handles.sensname(handles.sensname > 0) );
end
for i=1:str2num(get(handles.nosensorentry_tag,'string'))
if i<26
handles.sensname(i) = uicontrol('style', 'edit','Position', [5, b-(i-1)*20, 180, 17],'BackgroundColor',[0.941 0.922 0.557]);
handles.sensnot(i) = uicontrol('style', 'edit','Position', [385, b-(i-1)*20, 45, 17],'BackgroundColor',[0.941 0.922 0.557]);
handles.sensvtsno(i) = uicontrol('style', 'edit','Position', [510, b-(i-1)*20, 40, 17],'BackgroundColor',[0.941 0.922 0.557],'string',i);
handles.sensmeasno(i) = uicontrol('style', 'edit','Position', [625, b-(i-1)*20, 40, 17],'BackgroundColor',[0.941 0.922 0.557]);
guidata(hObject, handles)
else
handles.sensname(i) = uicontrol('style', 'edit','Position', [188, b-(i-26)*20, 180, 17],'BackgroundColor',[0.886 0.855 0.612]);
handles.sensnot(i) = uicontrol('style', 'edit','Position', [435, b-(i-26)*20, 45, 17],'BackgroundColor',[0.886 0.855 0.612]);
handles.sensvtsno(i) = uicontrol('style', 'edit','Position', [555, b-(i-26)*20, 40, 17],'BackgroundColor',[0.886 0.855 0.612],'string',i);
handles.sensmeasno(i) = uicontrol('style', 'edit','Position', [675, b-(i-26)*20, 40, 17],'BackgroundColor',[0.886 0.855 0.612]);
guidata(hObject, handles)
end
end
end
Please, Karthik, stop writing "does not work" without explaining what happens: if there is an error message, post a complete copy. If the results differ from your expectations, explain what you want and what you get with all necessary details.
I said "(no loop needed)". Change
for i=1:handles.count
delete(handles.sensname(handles.sensname > 0) );
end
to
delete(handles.sensname(handles.sensname > 0) );
handles.sensname(:) = 0; %clear them afterwards so old value is not hanging around next time you execute
Hi Walter,
You are correct, i was initialising with higher number of entries , now i corrected it . Now this code is working. Thanks alot Walter,Sean,Jan
for i=1:20
delete(handles.sensname(i));
end
More efficient would be
delete(handles.sensname(1:20));
with no loop (just the one line.)

Sign in to comment.

More Answers (1)

That means that handles.sensname(i) is returning a 0, the handle to the root object.
It it kind of hard to tell you what is broken with it, but perhaps you could just check for zeros first.
for ii = 1:10;
if logical(handles.sensname(ii))
delete(handles.sensname(ii));
end
end
More Note also that if I run those two lines of code back2back I don't get the error:
figure;
for i=1:20
handles.sensname(i) = uicontrol('style', 'edit','Position', [95, pi-(i-1)*20, 180, 17],'BackgroundColor',[1 1 1]);
end
for i=1:20
delete(handles.sensname(i));
end

5 Comments

it doesnt work. I used this example
figure
u=uicontrol('style','text');
delete(u).
Since i am referencing in handles, it is showing the error.
Or: if handles.sensname(ii) ~= 0
@Karthik: What did not work and which error message do you get?
The problem is *not* related to the fact, that the handle of the UICONTROL is stored in a struct called "handles". Therefore I do not understand, what "referencing in handles" means and how it could affect the error.
I am not quite sure about my understanding towards handles, as i shown the example delete(u) will delete the edit box got created. But if I am deleting the edit boxes created using the gui handles, i could not able to delete the created edit boxes.
Referencing in handles, i thought handle of the uicontrol cant be deleted.
Any handle graphics object can be deleted _except_ for the root object (0). This includes uicontrol() and text() and plots and surf() and so on.

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!