MATLAB: Iam browsing images from a folder using GUI Matlab. Now I want create a next button so that i can obtain all pics by pressing next. How to do that

gui matlab

The code I used for browsing was
[filename,pathname]=uigetfile({'*.jpg';'*.png';'*.tif'},'File selector');
image=strcat(pathname,filename);
axes(handles.axes1);
imshow(image);
set(handles.edit2,'String',filename);
set(handles.edit4,'String',image);

Best Answer

function next_Callback(hObject, event, handles)
curfile = get(handles.edit4,'String');
if isempty(curfile)
uiwait(msgbox('You need to have selected a file first before you can Next'));
return
end
[pathname, basename, ext] = fileparts(curfile);
dinfo = dir( fullfile(pathname, ['*' ext]) );
filenames = fullfile(pathname, {dinfo.name});
[tf, curidx] = ismember(curfile, filenames);
if ~tf
uiwait(msgbox('Current file not found in directory, do not know which one is Next'));
return;
end
if curidx == length(filenames)
uiwait(msgbox('At end of directory, no Next to go to'));
return;
end
nextfile = filenames{curidx+1};
[~, nextbase, nextext] = fileparts(nextfile);
nextname = [nextbase nextext];
set(handles.edit2, 'String', nextname);
set(handles.edit4, 'String', nextfile);
imshow(handles.axes1, nextfile);