How to cycle through images in a GUI

10 views (last 30 days)
Kylee
Kylee on 4 Jun 2014
Answered: Geoff Hayes on 4 Jun 2014
I am extremely rusty in creating GUIs. I have a massive amount of generate PNG images that need to be proofed by the user for validity. I need to be able to choose a root directory and the initial image that will be displayed. From here, I need to have the user be able to navigate through the folder and choose images that need to be corrected. The chosen images need to be copied and saved to another folder.
I understand how to use uigetfile to load in a single image, but I do not want to have the user navigate through that for every image. I want simple 'back' and 'next' push buttons to do this.
Is there a way to create a text file of the file names that I can use a loop to determine what file to load next? Is there a better way to do this?

Answers (2)

Joseph Cheng
Joseph Cheng on 4 Jun 2014
what you can do instead of generating a temporary text is use a listbox
function pushbutton1_Callback(hObject, eventdata, handles)
handles.folder = uigetdir; %store the selected folder in handles.folder;
files = dir(fullfile(handles.folder,'*png')); %get all png files
for i =1:length(files)
piclist{i} = files(i).name; %lists each of them in a cell
end
set(handles.listbox1,'String',piclist); %display them in the list box.
guidata(hObject, handles); %update the gui to save handles
function listbox1_Callback(hObject, eventdata, handles)
list= get(handles.listbox1,'string'); %get the picture list
selected = get(handles.listbox1,'value'); % get which one is selected.
I = imread(fullfile(handles.folder,cell2mat(list(selected)))); %open the picture
axes(handles.axes1);
image(I); %display image.
axis off;
the above is a quick functions if you have a guide gui with a pushbutton, listbox and axes. Here you push the button to select the folder and using dir it will select *.png files in that folder. it will then list them in the listbox and depending on which one you click you'll see that displayed.
Now if you want to use the next and back push buttons you can modify the above to save the list of images into variable that you access with the copy/export to another folder button.

Geoff Hayes
Geoff Hayes on 4 Jun 2014
An alternative - once the directory has been determined - is to use dir to grab the files from that directory and create a list (rather than you having to build a text file). If dirName is the variable for the chosen directory then
% verify that the directory is valid
if isdir(dirName)
% grab the contents from this directory
contents = dir(dirName);
% create a cell array of filenames (and paths to)
files = cell(length(contents),1); % default the size of the cell array
k=1;
for i=1:length(contents)
filename = fullfile(dirName,contents(i).name); % create the file source (path and name)
[path, name, ext] = fileparts(filename); % do this to get the extension
if ~isempty(ext) && strcmpi(ext,'.png')
k = k + 1;
files{k} = filename; % add the file to the cell array
end
end
files = files(1:k); % truncate array if not all slots used
end
The files cell array can then be saved to the handles structure (see guidata for help on this) and will be available within the callbacks of your forward and reverse arrows.

Categories

Find more on Dialog Boxes 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!