MATLAB: How to load the latest picture automatically

load latest

I have a program to vertically integrate the intensity of pictures. There is a push button that allows me to load the latest the picture in the directory. However, if I just run the program, no file is loaded at the beginning. I am wondering if there is a way to automatically load the latest file when I click run the program. Thanks!

function photobooth
defaultfileDir = 'C:\Users\physlabuser\Pictures\Camera Roll'; %This path
directory changes to reflect user
%defaultfileDir = [pwd, 'C:\Users\physlabuser\Pictures\Camera Roll'];
default_ub = 300;
default_lb = 200;
buttonHeight = 0.03;
buttonWidth = 0.08;
spacing = 0.01;
row1 = 0.94;
row2 = 0.90;
row3 = 0.86;
row4 = 0.82;
row5 = 0.78;
panelHeight = (row1 - row5) + 3*spacing + buttonHeight;
browseWidth = 0.04;
% specific to ROI section
col0 = 0.95 - buttonWidth - 2*spacing - browseWidth - 0.38;
col05 = (col0 + buttonWidth + spacing);
col1 = 0.95 - buttonWidth - spacing - browseWidth;
col2 = 0.95 - buttonWidth;
fieldWidth = 0.12;
nameWidth = 0.38 - buttonWidth - spacing;
panelWidth = (col2 - col0) + 2*spacing + buttonWidth;
% specific to fitting section
structWidth = 4*buttonWidth + 2*spacing - browseWidth - spacing;
fcol0 = 0.05;
fcol1 = 0.05 + buttonWidth + spacing;
fcol2 = 0.05 + 2*buttonWidth + 2*spacing;
fbrowsecol = fcol1 + structWidth + spacing;
fcol3 = fbrowsecol + browseWidth + spacing;
fpanelWidth = (fcol3 - fcol0) + 2*spacing + buttonWidth;
mainX = 0.05;
mainY = 0.3;
mainW = 0.9;
mainH = 0.55;
histX = 0.05;
histY = 0.05;
histW = 0.9;
histH = 0.2;
hMainFigure = figure(...
'Visible','off',...
'NumberTitle','off',...
'Name','Some random vertically integrating program',...
'Toolbar','figure',...
'Position',[128,128,864,864]);
%Make the GUI visible.
set(hMainFigure,'Visible','on')
% Construct the components
ah = axes(...
'Parent',hMainFigure,...
'ButtonDownFcn', @get_cursor_pos, ...
'Position',[mainX mainY mainW mainH]);
histogram = axes(...
'Parent',hMainFigure,...
'Position',[histX histY histW histH]);
imagefieldtext = uicontrol(...
'Parent', hMainFigure, ...
'Units','normalized',...
'Style', 'text', ...
'String','image location',...
'HorizontalAlignment', 'center', ...
'Position',[fcol0 row1 buttonWidth buttonHeight]);
imagefield = uicontrol(...
'Style','edit',...
'Parent', hMainFigure, ...
'String','' ,...
'Units','normalized',...
'Max',1,'Min',0,...
'Callback', @UpdateCallback,...
'Position',[fcol1 row1 structWidth buttonHeight]);
imageBrowseButton = uicontrol(... % button for updating selected plot
'Parent', hMainFigure, ...
'Units','normalized',...
'HandleVisibility','callback', ...
'Position',[fbrowsecol row1 browseWidth buttonHeight],...
'String','...',...
'Callback', @imageBrowseCallback);
loadLatestButton = uicontrol(... % button for updating selected plot
'Parent', hMainFigure, ...
'Units','normalized',...
'HandleVisibility','on',...
'Position',[fcol3 row1 buttonWidth buttonHeight],...
'String','Load Latest',...
'Callback', @loadLatestImage);
fileDirtext = uicontrol(...
'Parent', hMainFigure, ...
'Units','normalized',...
'Style', 'text', ...
'String','Camera Roll dir',...
'HorizontalAlignment', 'center', ...
'Position',[fcol0 row2 buttonWidth buttonHeight]);
fileDirField = uicontrol(...
'Style','edit',...
'Parent', hMainFigure, ...
'String',defaultfileDir,...
'Units','normalized',...
'Max',1,'Min',0,...
'Callback', @fileDirUpdateCallback,...
'Position',[fcol1 row2 structWidth buttonHeight]);
fileDirBrowseButton = uicontrol(... % button for updating selected plot
'Parent', hMainFigure, ...
'Units','normalized',...
'HandleVisibility','callback', ...
'Position',[fbrowsecol row2 browseWidth buttonHeight],...
'String','...',...
'Callback', @hfileDirBrowseCallback);
directionstext = uicontrol(...
'Parent', hMainFigure, ...
'Units','normalized',...
'Style', 'text', ...
'String','use cursor to select point and then hit "Set lb" ("Set ub") to specify lower (upper) integration bounds.',...
'HorizontalAlignment', 'center', ...
'Position',[col0 + 2*buttonWidth row2 0.2 2*buttonHeight + spacing]);
lbButton = uicontrol(... % button for updating selected plot
'Parent', hMainFigure, ...
'Units','normalized',...
'HandleVisibility','callback', ...
'Position',[col2 row1 buttonWidth buttonHeight],...
'String','Set lb',...
'Callback', @getlb);
ubButton = uicontrol(... % button for updating selected plot
'Parent', hMainFigure, ...
'Units','normalized',...
'HandleVisibility','callback', ...
'Position',[col2 row2 buttonWidth buttonHeight],...
'String','Set ub',...
'Callback', @getub);
lbfield = uicontrol(... % button for updating selected plot
'Style','edit',...
'Parent', hMainFigure, ...
'Units','normalized',...
'HandleVisibility','callback', ...
'Position',[col1 row1 browseWidth buttonHeight],...
'String',num2str(default_lb),...
'Callback', @loadImage);
ubfield = uicontrol(... % button for updating selected plot
'Style','edit',...
'Parent', hMainFigure, ...
'Units','normalized',...
'HandleVisibility','callback', ...
'Position',[col1 row2 browseWidth buttonHeight],...
'String',num2str(default_ub),...
'Callback', @loadImage);
data = guihandles(hMainFigure);
data.fileDir = defaultfileDir;
data.ub = default_ub;
data.lb = default_lb;
guidata(hMainFigure, data)
%%%%%%%callbacks %%%%%%%%%
function hfileDirBrowseCallback(hObject, eventdata)
% Callback function run when the update button is pressed

foldername = uigetdir(pwd, ...
'Select Photo Booth directory.');
if foldername == 0
else
set(fileDirField, 'String', foldername);
data = guidata(gcbo);
data.fileDir = foldername;
guidata(gcbo, data);
end
end
function UpdateCallback(hObject, eventdata)
% Callback function run when the update button is pressed
loadImage(imagefield, eventdata)
end
function fileDirUpdateCallback(hObject, eventdata)
% Callback function run when the update button is pressed
foldername = get(fileDirField, 'String');
data = guidata(gcbo);
data.fileDir = foldername;
guidata(gcbo, data);
end
function imageBrowseCallback(hObject, eventdata)
% Callback function run when the update button is pressed
data = guidata(gcbo);
[filename pathname] = uigetfile(fullfile(data.fileDir, '*.jpg'), ...
'Select image to display.');
if filename == 0
else
imagename = fullfile(pathname, filename);
set(imagefield, 'String', imagename);
loadImage(imagefield, eventdata)
end
end
function loadLatestImage(hObject, eventdata)
% Callback function run when the update button is pressed
data = guidata(gcbo);
files = dir(fullfile(data.fileDir,'*.jpg'));
if numel(files) == 0
warndlg('No JPG files exist in Camera Roll directory.')
else
dates = [files(:).datenum];
[sorted_dates date_order] = sort(dates);
sorted_files = files(date_order);
files = sorted_files;
% for i = 1:numel(files)
% disp('---------new image---------')
% files(i).name
% files(i).date
% files(i).datenum
% end
%
set(imagefield, 'String', fullfile(data.fileDir, files(end).name));
loadImage(imagefield, eventdata)
end
end
function get_cursor_pos(hObject, eventdata)
cursor_pos = get(ah, 'Currentpoint');
end
function getub(hObject, eventdata)
cursor_pos = get(ah, 'Currentpoint');
set(ubfield, 'String', num2str(round(cursor_pos(1, 2, 1))));
data = guidata(gcbo);
data.ub = str2num(get(ubfield, 'String'));
guidata(gcbo, data);
loadImage(imagefield, eventdata)
end
function getlb(hObject, eventdata)
cursor_pos = get(ah, 'Currentpoint');
set(lbfield, 'String', num2str(round(cursor_pos(1, 2, 1))));
data = guidata(gcbo);
data.lb = str2num(get(lbfield, 'String'));
guidata(gcbo, data);
loadImage(imagefield, eventdata)
end
function loadImage(field, eventdata)
imagename = get(imagefield, 'String');
[X map] = imread(imagename);
%imagesc(X, 'Parent', ah, 'HitTest', 'off')
imagesc(X, 'Parent', ah);
set(ah, 'ButtonDownFcn', @get_cursor_pos);
data = guidata(gcbo);
data.lb = str2num(get(lbfield, 'String'));
data.ub = str2num(get(ubfield, 'String'));
guidata(gcbo, data);
if data.lb > 0 && data.ub > 0
vert_hist = sum(X(data.lb:data.ub, :, 1), 1);
vert_hist = vert_hist + sum(X(data.lb:data.ub, :, 2), 1);

Best Answer

After you call
guidata(hMainFigure, data)
then call
loadLatestImage(loadLatestButton, [])
and in loadLatestImage change
data = guidata(gcbo);
to
data = guidata(hObject);
(because for that direct call there will not be a callback object.)