- Upgrade to R2016b or later, which allows you to put function definitions at the end of a script file; Or
- put a "function" statement at the beginning of the file, so that the file would no longer start with a script; Or
- move the code for listbox1_Callback to the file listbox1_Callback.m so that your functions are separated from your script.
How to create listbox using uicontrol and not using Matlab GUIDE?
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Aleksandar Petrov
am 20 Jun. 2017
Kommentiert: Walter Roberson
am 20 Jun. 2017
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
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 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:
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 Kommentare
Walter Roberson
am 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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!