Cannot break the while loop due to imagedatastore
3 views (last 30 days)
Show older comments
Marcin Kowalski
on 14 Mar 2023
Edited: Marcin Kowalski
on 15 Mar 2023
Hi all,
I have build a simple app (appdesigner) which looks for new images in specific network storage and processess the data.
Since I am doing the searching for new files in a while loop I wand to be able to break when a stop button is pushed. The code looks following:
The issue I have is that the code works well till I add the dir_content2 in a while loop. Since then I cannot close the app other way than ctrl+C.
It looks that using imagaDatastore object to check the directory content is the issue. Any ideas what is happening?
EDIT: I found that when I use pause(0.1) at the end of the loop, it is possible to do do the break.
properties (Access = public)
stop_sim = false;
modelName;
model;
net;
anchors;
classNames;
executionEnvironment;
folder;
dir_content;
filenames;
current_files;
DB_address;
DB_port;
tablename;
conn;
StopValue;
end
methods (Access = private)
function stopSimulation(app)
app.stop_sim = true;
msgbox('Application stopped');
app.delete;
end
function startSimulation(app)
i = 0;
ExitCount = 1;
% i=0;
counter = 1;
while i<=1000000 && app.stop_sim == false
i = i +1;
counter = 1;
counter = counter+1;
dir_content2 = imageDatastore(app.folder, "IncludeSubfolders",true);
%% processing parth goes here
end
end
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: StartButton
function StartButtonPushed(app, event)
app.status.Value = ('loading data');
app.StopValue = 0;
app.ImageAxes.Visible = 'off';
app.ImageAxes.Colormap = gray(256);
axis(app.ImageAxes, 'image');
%% YOLO model
%% configuration
%#exclude appconfig.txt
Config = fileread('appconfig.txt')
config = split(Config);
app.status.Value = ('waiting for new files');
app.folder = config{3};
app.DB_address = string(config{6});
DBport = config{9};
app.DB_port = str2double(DBport);
app.dir_content = imageDatastore(app.folder,"IncludeSubfolders",true);
app.filenames = app.dir_content.Files;
app.current_files = app.filenames;
%% database;
startSimulation(app)
end
% Button pushed function: StopButton
function StopButtonPushed(app, event)
stopSimulation(app)
end
end
% Component initialization
methods (Access = private)
% App creation and deletion
methods (Access = public)
% Construct app
function app = IMAD_app_07
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
0 Comments
Accepted Answer
Adam Drake
on 14 Mar 2023
Edited: Adam Drake
on 14 Mar 2023
What is the purpose of your while loop? I would suggest using a for loop instead of a counter OR flag app.stop_sim programatically whenever you finish processing. If you store all of the images in your directory, there's no need to do it again, much less 10000000 times.
% Example
imds = imageDatastore(app.folder, "IncludeSubfolders",true);
for i = 1:length(imds)
img = readimage(imds,i)
end
3 Comments
Adam Drake
on 14 Mar 2023
Your RAM consumption is because you're reading every image in your directory 1000000 times.
- An image is added to your directory.
- You store it.
- You do processing.
Now you loop back regardless of if another image was produced and read it again. When another image is added to the directory, you're now storing 2 images everytime. This is not efficient coding. You need to have a function run processing whenever an image is added to the directory and only store that one image. You need to look at appending to the imageDatastore object when a new image is added to the directory.
To detect when a new image is added to the directory SEE https://www.mathworks.com/matlabcentral/answers/18232-detect-new-file-in-a-directory
If I have answered your question, please Accept. Thanks!
More Answers (0)
See Also
Categories
Find more on Develop uifigure-Based Apps 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!