Is this the proper way of using ListBox?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Ba Ba Black Sheep!
am 1 Okt. 2017
Bearbeitet: Jan
am 1 Okt. 2017
I am learning Matlab GUI programming. I have written the following basic program to test ListBox GUI control.
The following program loads several image-files in the memory and are displayed in an Axes-GUI control according to the selection of the ListBox by mouse-clicking.
function varargout = ListBox__Test(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @ListBox__Test_OpeningFcn, ...
'gui_OutputFcn', @ListBox__Test_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before ListBox__Test is made visible.
function ListBox__Test_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
image_name_cell_array = {'gif1.gif', 'jpg1.jpg', 'png1.png', 'png2.png'};
handles.image_name_cell_array = image_name_cell_array;
handles.image_cell_array = {imread(image_name_cell_array{1}), ...
imread(image_name_cell_array{2}), ...
imread(image_name_cell_array{3}), ...
imread(image_name_cell_array{4})};
set(handles.listbox1,'String',image_name_cell_array);
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line.
function varargout = ListBox__Test_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
selected_index = get(hObject,'Value');% selected index
I = handles.image_cell_array{selected_index}; % obtain selected item
axes(handles.axes1);
image(I);
% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
Is this the proper way to do what I am trying to accomplish? Or, Is there any better way to do this?
0 Kommentare
Akzeptierte Antwort
Jan
am 1 Okt. 2017
Bearbeitet: Jan
am 1 Okt. 2017
Yes. This is a proper way.
It would be tiny bit safer to replace
axes(handles.axes1);
image(I);
by
image(handles.axes1, I);
If the axes() command is ready and the user clicks on another axes before the image() command is called, the image appears in the wrong container. The time slot for this potential problem is extremely small, but the idea is never to change the current object, but to provide the parent object explicitly in every case.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Graphics Object Properties 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!