matlab gui list box

2 views (last 30 days)
koklan majid
koklan majid on 28 Apr 2017
Commented: Walter Roberson on 30 Apr 2017
function varargout = m(varargin)
% MIDTERM2017 MATLAB code for m.fig
%MIDTERM2017, by itself, creates a new MIDTERM2017 or raises the existing
% singleton*.
%
% H = MIDTERM2017 returns the handle to a new MIDTERM2017 or the handle to
% the existing singleton*.
%
% MIDTERM2017('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MIDTERM2017.M with the given input arguments.
%
% MIDTERM2017('Property','Value',...) creates a new MIDTERM2017 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before midterm2017_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to midterm2017_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help midterm2017
% Last Modified by GUIDE v2.5 20-Apr-2017 19:17:54
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @midterm2017_OpeningFcn, ...
'gui_OutputFcn', @midterm2017_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 midterm2017 is made visible.
function midterm2017_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to midterm2017 (see VARARGIN)
SelectedItem = findobj('Tag','listbox1');
t=timer('Period',10,'ExecutionMode','fixedRate');
t.TimerFcn={@listUpdateFcn, handles};
start(t)
% Choose default command line output for midterm2017
handles.output = hObject;
setappdata(handles.figure1,'Timer',t)
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes midterm2017 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = midterm2017_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --------------------------------------------------------------------
function Untitled_1_Callback(hObject, eventdata, handles)
% hObject handle to Untitled_1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
showAbout(1,2)
% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns listbox1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from listbox1
contents = cellstr(get(hObject,'String'));
imageFile = contents{get(hObject,'Value')};
im=imread(imageFile);
imshow(im,'Parent',handles.axes2)
set(handles.listbox1,'Value',1)
% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes during object deletion, before destroying properties.
function figure1_DeleteFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
t = getappdata(handles.figure1,'Timer');
stop(t)
delete(t)
delete(hObject)
function listUpdateFcn(o, e, handles)
imf = imformats;
imFiles=[imf.ext];
files = dir('*.*');
names = {files.name};
imageFileIndices = contains(names,imFiles);
set(handles.listbox1,'String',names(imageFileIndices))
listLength = length(handles.String); % SelectedItem yenilendiðinde hata oluþmamasý için
if handles.Value > listLength
handles.Value = listLength;
end
%guidata(handles.figure1, handles);
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
folder_name = uigetdir();
% get what is inside the folder
Infolder = dir(folder_name);
imageFile = {Infolder(~[Infolder.isdir]).name};
set(handles.listbox1,'String', imageFile);
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1
img_color =getimage(handles.axes2);
color = handles.popupmenu1.Value;
im_red = img_color;
im_green = img_color;
im_blue = img_color;
if color == 1
imshow(img_color,'Parent',handles.axes2); % RGB renk
end
if color == 2
im_red(:,:,2) = 0;
im_red(:,:,3) = 0;
imshow(im_red,'Parent',handles.axes2);
end
if color == 3
im_green(:,:,1) = 0;
im_green(:,:,3) = 0;
imshow(im_green,'Parent',handles.axes2);
end
if color == 4
im_blue(:,:,1) = 0;
im_blue(:,:,2) = 0;
imshow(im_blue,'Parent',handles.axes2);
end
% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

Answers (2)

Image Analyst
Image Analyst on 29 Apr 2017
You don't want handles.Value, you want handles.listbox1.Value.
This GUI will help the novice user get up to speed very quickly on using GUI-based applications. Everything is laid out in a very simple Step 1, Step 2, Step 3, etc. layout. It is a very good starting point for a typical image analysis application. This application uses GUIDE to do the user interface design, and has most of the basic controls such as buttons, listboxes, checkboxes, radio buttons, scrollbars, etc. It allows the user to select a folder of images, select one or more images and display them, to select a series of options, and to individually or batch process one or more images. The user can.....
  8 Comments
Image Analyst
Image Analyst on 29 Apr 2017
I don't have anything like that coded up and ready to give it to you. So one of us would have to do that and I think it should be you. Sorry. Also look at the function addlistener(), which might help.
Walter Roberson
Walter Roberson on 30 Apr 2017
Did koklan majid delete some comments? I do not seem to have a context for some of what Image Analyst replied.

Sign in to comment.


Walter Roberson
Walter Roberson on 29 Apr 2017
In pushbutton1_Callback you do a uigetdir(), and you dir() the folder indicated, recording only the relevant file names in the listbox.
In listUpdateFcn you look in the current directory, find the files, and update the listbox.
In listbox1_Callback, you extract the selected string from the listbox, and imread() that.
Problem: what you record in the listbox does not have the path attached to it, and you do not record the path to add it on to the names. You might have let the user choose which other directory to get file names from, but then you attempt to read those files from the current directory.
You should be saving a copy of the chosen directory name in pusbutton1_Callback, and you should be using that directory name when you imread() the file.
You should probably use fullfile() along the way.
  3 Comments
Walter Roberson
Walter Roberson on 29 Apr 2017
After
set(handles.listbox1,'String', imageFile);
do
set(handles.listbox1,'UserData', folder_name);
Change
im=imread(imageFile);
to
im = imread( fullfile( get(hObject, 'UserData'), 'imageFile);
koklan majid
koklan majid on 29 Apr 2017
still having the same problem when i change the path direction the Listbox disappear

Sign in to comment.

Categories

Find more on Environment and Settings 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!