Cannot break the while loop due to imagedatastore

조회 수: 1 (최근 30일)
Marcin Kowalski
Marcin Kowalski 2023년 3월 14일
편집: Marcin Kowalski 2023년 3월 15일
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

채택된 답변

Adam Drake
Adam Drake 2023년 3월 14일
편집: Adam Drake 2023년 3월 14일
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
Adam Drake
Adam Drake 2023년 3월 14일
Your RAM consumption is because you're reading every image in your directory 1000000 times.
  1. An image is added to your directory.
  2. You store it.
  3. 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!
Marcin Kowalski
Marcin Kowalski 2023년 3월 15일
Thank you answer.
I replaced imageDatastore with dir and looks more effective in terms of RAM consumption, for sure. To break, pause was needed.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by