Is this the proper way of using ListBox?
조회 수: 1 (최근 30일)
이전 댓글 표시
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
채택된 답변
Jan
2017년 10월 1일
편집: Jan
2017년 10월 1일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Object Properties에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!