MATLAB: Send data to main gui after pushbutton

get handlesgui

I have made a script that draw lines in a picture inside a gui. For each line it calculates its distance. Now I want to make a function that can delete some of the lines. The amount of lines is defined by "nlines"
What I have is a pushbutton that generates a new window with a checkbox for each created line and a delete_button. When pushing this button, the respecting lines have to be deleted and "nlines" have to be decreased. I do get the data inside of the pushbutton_deletebutton function. But cannot get the handles back into the main gui.
I have the following:
function ClearMeasuresButton_Callback(hObject, eventdata, handles)
% hObject handle to ClearMeasuresButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
nlines = handles.nlines; %Amount of lines
if nlines == 0
msgbox('There are no measures yet')
return
end
distance = handles.distance; %Distances of lines
fig = figure('Visible','off');
box = zeros(nlines,1);
txtbox = zeros(nlines,1);
panel = uipanel('parent',fig,...
'Title','Choose lines to delete',...
'position',[.01 .02 .95 .95]);
A=zeros(nlines,1);
label1 = uicontrol('parent',panel,'style','text',...
'Position',[20 nlines*25+60 150 30],...
'String',{'Choose the measurement';'you want to delete'});
for i = 1:nlines
f.box(i) = uicontrol('parent', panel,'style','checkbox', ...
'tag', sprintf('checkbox%d', i),...
'position',[20 nlines*25+60-i*25 40 20]);
txtbox = uicontrol('parent', panel,'Style','text',...
'String',[num2str(i), ' : ', num2str(Distance(i),3), ' [m]'],...
'Position',[40 nlines*25+60-i*25 100 20]);
end
handles.f = f;
deletebutton = uicontrol('Parent', panel, 'style', 'pushbutton',...
'tag', 'deletebutton','String', 'Delete',...
'callback', {@Pushbutton_deletebutton, handles},...
'position',[20 20 60 30]);
set(fig, 'Visible', 'on', 'Position',[1000 450-nlines*25 200 nlines*25+130]);
guidata(hObject,handles)
And the function for the Pushbutton that deletes the lines.
function handles = Pushbutton_deletebutton(hObject, eventdata, handles)
f = handles.f;
nlines = handles.nlines
distance = handles.distance
for k = 1:length(f.box)
val(k) = f.box(1,k).Value; %Values of the checkboxes
end
for j = length(val):-1:1
if val(j) == 1
distance(j) = [];
nlines = nlines-1;
end
end
handles.distance = distance;
handles.nlines = nlines;
guidata(hObject,handles)

Best Answer

Provide the handle of the calling figure to the sub-GUI, e.g. by:
function ClearMeasuresButton_Callback(hObject, eventdata, handles)
% Add:
mainGUI = ancestor(hObject, 'figure'); % There must be a field in the handles struct also.
...
% Then provide it e.g. by the figure's UserData:
set(fig, 'Visible', 'on', 'Position',[1000 450-nlines*25 200 nlines*25+130], ...
'UserData', mainGUI);
Then in the sub GUI:
function Pushbutton_deletebutton(hObject, eventdata, handles)
subGUI = ancestor(hObject, 'figure');
mainGUI = get(subGUI, 'UserData');
mainGUI_handles = guidata(mainGUI);
% But remember: You have defined this callback using the handles
% struct of the main GUI! So the handles and mainGUI_handles
% are identical.
...
distance = handles.distance;
val = [handles.f.box.Value];
handles.distance = distance(val ~= 1);
handles.nlines = handles.nlines - sum(val == 1);
% No, do not change the handles of the *current* figure:
% guidata(hObject,handles)
% But of the main GUI:
guidata(mainGUI, handles);
You do not have to ask ancestor to get the handle of the figure, but the handles struct should contain the handle of the figure already. I do not know, how it is called, because I'm not working with GUIDE.
Related Question