Borrar filtros
Borrar filtros

How can I use more than one function with inputs and outputs in a uicontrol button callback?

7 visualizaciones (últimos 30 días)
I am trying to create an interface to select fruits from a list:
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...

Respuesta aceptada

Stephen23
Stephen23 el 26 de Mayo de 2018
Editada: Stephen23 el 26 de Mayo de 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.

Más respuestas (0)

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by