How to create listbox using uicontrol and not using Matlab GUIDE?
Show older comments
Hello,
I am trying the create listbox and text box using uicontrol and not using GUI! so anytime I click on one of the list items I get this error message 'Error: Function definitions are not permitted in this context'. I believe there is a problem with the definition of the Callback function. here is the code;
c = dialog('Position',[300 300 250 150],'Name','Select One');
txt = uicontrol('Parent',c,...
'Style','text',...
'Position',[75 120 100 15],...
'HorizontalAlignment','left',...
'String','Selected color is:');
listbox1 = uicontrol('Parent',c,...
'Style','listbox',...
'Position',[75 70 100 50],...
'Max',6,...
'String',{'Red';'Green';'Blue'},...
'Callback', @listbox1_Callback);
%
function listbox1_Callback(hObject, eventdata, handles)
file_list = get(handles.listbox1,'String');
index_selected = get(handles.listbox1,'Value');
filename = file_list{index_selected};
set(handles.txt,'String', filename);
end
Usually, when uicontrol function is called, the key-word 'Callback' refers to a function @NameOfTheFunction_Callback. this function must latter be defined so as the uicontrol could work as desired
function NameOfTheFunction_Callback (hObject, eventdata, handles)
Here you code the task this uicontrol performs anytime you activate it
end
what is wrong? Thank you
Accepted Answer
More Answers (0)
Categories
Find more on Interactive Control and Callbacks 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!