How to create listbox using uicontrol and not using Matlab GUIDE?

15 views (last 30 days)
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

Walter Roberson
Walter Roberson on 20 Jun 2017
It is not possible to have a uicontrol without using a GUI. That is like asking to use a triangle without using a shape: a uicontrol is an element of a GUI so as soon as you make a uicontrol you have a GUI by definition, and so you cannot have a uicontrol without a GUI.
Perhaps you mean that you want to do it without using GUIDE. If so then I predict that you are using R2016a or earlier, and that you are trying to store all of the code you show in a single .m file. You need to do one of the following:
  1. Upgrade to R2016b or later, which allows you to put function definitions at the end of a script file; Or
  2. put a "function" statement at the beginning of the file, so that the file would no longer start with a script; Or
  3. move the code for listbox1_Callback to the file listbox1_Callback.m so that your functions are separated from your script.
I notice that you are trying to use the handles structure in listbox1_Callback . That is not a problem if you are using GUIDE to construct the GUI, but if you are programming it without GUIDE, then you have a problem. The handles structure is not automatically passed to functions by MATLAB. GUIDE adds on some extra layers that are responsible for creating a handles structure and passing the handles structure to functions, so if you want to program without GUIDE you need to build the structure and pass it around yourself.
For example you could code,
function myGUI
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, txt});
function listbox1_Callback(hObject, eventdata, txt)
file_list = get(hObject, 'String');
index_selected = get(hObject, 'Value');
filename = file_list{index_selected};
set(txt, 'String', filename);
  2 Comments
Aleksandar Petrov
Aleksandar Petrov on 20 Jun 2017
Thank you for your prompt answer!
You are right, I meant GUIDE and not GUI (uicontrols create GUI same as the GUIDE, they are two different approaches for creating GUIs).
It works both as a separate function and integrated in a code.
Regarding the function listbox1_Callback, I also tried to use hObject instead of handles but still was not working. as I assumed, the problem was somewhere in the definition of the Callback function. in fact it requires definition of an additional argument in the function so as the selected item be shown in the text box.
My question is, what if I want to show the selected item in two different text box? does it mean I must add an second argument in the function definition? something like this:
'CallBack', {@listbox_callback, txt1, txt2});
Thank you very much
Best Regards,
Aleksandar Petrov
Walter Roberson
Walter Roberson on 20 Jun 2017
'CallBack', {@listbox_callback, [txt1, txt2]});
With this change you would not even need to change the callback code, as the single set(txt, 'String', filename) would set the String properties of both objects. If you needed to treat them differently, you could index in the callback, txt(1), txt(2) and so on.

Sign in to comment.

More Answers (0)

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!