How can I use more than one function with inputs and outputs in a uicontrol button callback?
1 view (last 30 days)
Show older comments
I am trying to create an interface to select fruits from a list:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/190844/image.png)
The Select All and Clear Selected buttons are working, but I don't know how to do the Done button. I want the Done button to copy the check boxes value, and then close the figure.
This is my code up to now. The Done button part is the one not working.
function Test
handles.f.Fruits = {'Apple';'Pear';'Blueberry';'Avocado';'Tomato'};
handles.f.Fig = figure('NumberTitle','off','MenuBar','none');
handles.f.Msg1 = uicontrol('Parent',handles.f.Fig,'Style',...
'text','FontSize',14,'HorizontalAlignment','left','String',...
'Select fruits','Units','normalized','Position',...
[0.05 0.87 0.8 0.1]);
handles.f.Tab1 = uitable('Data',[num2cell(true(length(handles.f.Fruits),1)) handles.f.Fruits], ...
'ColumnEditable',[true false],'ColumnName',[],'Units','normalized',...
'Position',[0.05 0.14 0.9 0.78],'FontSize',14);
handles.f.btn1 = uicontrol('Parent',handles.f.Fig,'Style',...
'pushbutton','Units','normalized','Position',[0.1 0.07 0.3 0.06],...
'FontSize',14,'String','Select All','Callback',{@SelectAll,handles});
handles.f.btn2 = uicontrol('Parent',handles.f.Fig,'Style',...
'pushbutton','Units','normalized','Position',[0.6 0.07 0.3 0.06],...
'FontSize',14,'String','Clear Selected','Callback',...
{@ClearSelected,handles});
handles.f.btn3 = uicontrol('Parent',handles.f.Fig,'Style',...
'pushbutton','Units','normalized','Position',[0.35 0.01 0.3 0.06],...
'FontSize',14,'String','Done','Callback','handles = SaveData(handles); delete(gcf)');
waitfor(handles.f.Fig)
% Print fruits
function SelectAll(hObject,EData,handles)
set(handles.f.Tab1 ,'Data',...
[num2cell(true(length(handles.f.Fruits),1)) ...
handles.f.Fruits])
function ClearSelected(hObject,EData,handles)
set(handles.f.Tab1 ,'Data',...
[num2cell(false(length(handles.f.Fruits),1)) ...
handles.f.Fruits])
function handles = SaveData(handles)
handles.f.Chosen = get(handles.f.Tab1, 'Data');
Thank you very much for any help...
0 Comments
Accepted Answer
Stephen23
on 26 May 2018
Edited: Stephen23
on 26 May 2018
You question is fundamentally one of how to pass data between callback workspaces. As you have noticed, callback functions do not return any output arguments, but there are several other ways to pass data between callbacks, which you can learn about by reading the MATLAB documentation:
You should avoid evaluating strings (the documentation specifically advises "A character vector containing a valid MATLAB expression (not recommended)."), using global variables, or using evalin, assignin, etc. (unless you want to force yourself into writing slow, complex buggy code):
If you are programming your own GUI then I would highly recommend using nested functions to pass the data between callback workspaces: nested functions are simple and very intuitive because they have access to all of the variables in the main (parent) workspace, so you don't need to do anything extra to access them within a (nested) callback function. Thus for your code you could do something like this:
function out = Test()
out = {};
vec = {'Apple';'Pear';'Blueberry';'Avocado';'Tomato'};
fgh = figure('NumberTitle','off','MenuBar','none');
txh = uicontrol('Parent',fgh,'Style','text',...
'FontSize',14,'HorizontalAlignment','left','String',...
'Select fruits','Units','normalized','Position',[0.05 0.87 0.8 0.1]);
tbl = uitable('Data',[num2cell(true(length(vec),1)) vec], ...
'ColumnEditable',[true false],'ColumnName',[],'Units','normalized',...
'Position',[0.05 0.14 0.9 0.78],'FontSize',14);
bt1 = uicontrol('Parent',fgh,'Style','pushbutton',...
'Units','normalized','Position',[0.1 0.07 0.3 0.06],...
'FontSize',14,'String','Select All','Callback',@SelectAll);
bt2 = uicontrol('Parent',fgh,'Style','pushbutton',...
'Units','normalized','Position',[0.6 0.07 0.3 0.06],...
'FontSize',14,'String','Clear Selected','Callback',@ClearSelected);
bt3 = uicontrol('Parent',fgh,'Style','pushbutton',...
'Units','normalized','Position',[0.35 0.01 0.3 0.06],...
'FontSize',14,'String','Done','Callback',@SaveClose);
waitfor(fgh)
% nested functions:
function SelectAll(~,~)
set(tbl ,'Data',[num2cell(true(length(vec),1)),vec])
end
%
function ClearSelected(~,~)
set(tbl ,'Data',[num2cell(false(length(vec),1)),vec])
end
%
function SaveClose(~,~)
out = get(tbl,'Data');
close(fgh)
end
end % EOF
If you are using GUIDE then you should use guidata for passing the data between the callbacks, following the examples in the documentation.
More Answers (0)
See Also
Categories
Find more on Migrate GUIDE Apps 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!