Parallel processes in MATLAB App Designer

15 ビュー (過去 30 日間)
Anna
Anna 2023 年 10 月 13 日
コメント済み: Anna 2023 年 10 月 13 日
I am doing an app, which has a loop that is being performed each 10 seconds (it is an infinite loop with a flag to exit and has a 10 sec pause). Also, in the app I have a button which callback starts another matlab script. While the script is running, it is writing its "progress" to the .txt file (this is just a float type number).
The idea is that while the script is running, the app reads this number each 10 seconds from the .txt file and shows it in an edit field box. However, while the app is running, as I push the button which starts the matlab script, no changes in Edit Field appear, even though the numbers in the .txt file change. As soon as the matlab script finishes, Edit field shows 100% (the latest number). It seems like App Designer performs the whole matlab script first, while holding the loop on a pause, and starts it again only after the script is finished.
On some different websites I read that those processes should go in parallel (without any additional toolboxes). Is that right? What should I do in order to make Edit Field update "online" with the script processing?
I know that there is a command "drawnow", but I do not know, where to put it, since the loop is being frozen. Relevant parts of the code are below:
This is a loop:
function imaging(app)
progres_path = strcat(app.Directory,'/progres.txt'); %reaching for the .txt file
progres = fopen(progres_path, 'r'); %opening for reading
while ~(app.ExitLoopFlag) %exit loop flag (true before the app is being closed)
if isfile(strcat(app.Directory,'/New_Net/VGG_new_results.mat'))
load(strcat(app.Directory,'/New_Net/VGG_new_results.mat'),'FPR', 'TPR','AUC', 'val_accuracy', 'sensitivity', 'specificity');
cla(app.UIAxes,'reset');
app.UIAxes.GridAlpha = 1;
plot(app.UIAxes,FPR,TPR,'Color','r','LineWidth',2);
leg = strcat('AUC = ', num2str(AUC));
legend(app.UIAxes, leg);
xlabel(app.UIAxes,'False positive rate');
ylabel(app.UIAxes,'True positive rate');
title(app.UIAxes,'ROC Curve');
app.ExitLoopFlag = true;
app.AdddirectoriesButton.Enable = 'on';
app.RetrainthenetButton.Enable = 'on';
end
pause(10); %waiting for 10 seconds for the next loop iteration
percentage = fscanf(progres, '%s'); %reading data from file
result = strcat(percentage, ' %'); %forming a string
app.ProgresspreprocessingEditField.Value = result;
drawnow; %trying to push the changes (doesn't work)
end
fclose(progres); %closing the file
app.ProgresspreprocessingEditField.Value = 'Done!';
progres = fopen(progres_path, 'w');
fprintf(progres,'%s', '0');
fclose(progres);
percentage = '0.0';
progres = fopen(progres_path, 'r');
while str2double(percentage) < 99.99
percentage = fscanf(progres,'%s');
result = strcat(percentage, ' %');
app.ProgresstrainingEditField.Value = result;
drawnow;
end
fclose(progres);
app.ProgresstrainingEditField.Value = 'Done!';
end
This is the callback:
function RetrainthenetButtonPushed(app, event)
% Disable Plot Options button while dialog is open
app.RetrainthenetButton.Enable = 'off';
app.AdddirectoriesButton.Enable = 'off';
%starting preprocessing
preprocessing = strcat(app.Directory,'/preprocessing_ML_v3.m'); %matlab script
eval(['run(''' preprocessing ''');']); %starting the matlab script
end

回答 (1 件)

Julian
Julian 2023 年 10 月 13 日
編集済み: Julian 2023 年 10 月 13 日
As I see so far:
You open the file before your loop.
So you see a value, save this value and look everytime at the old value you have stored.
You have to open the file in your loop.
  1 件のコメント
Anna
Anna 2023 年 10 月 13 日
I moved open() and close() parts inside the loop, but it didn't help. Though, I was initially reading the value inside the loop, so it should have been updated anyway, right?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDevelop Apps Using App Designer についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by