Using 'while' loop to check if a file has been added to path
9 ビュー (過去 30 日間)
古いコメントを表示
Version: 2019b
I have an App Designer script in 2019b. I am trying to use a while loop to allow the user to add a file to the path before proceeding OR exit the app. The code is:
MFC_app_dir = fileparts(which('var.m'))
while length(MFC_app_dir) == 0
quit_choice = uiconfirm(app.GPSDB_Simulink, 'var.m not found in Matlab Path!', 'Exit Dialog', ...
'Options',{'Fixed, continue', 'Exit this app'}, ...
'DefaultOption',1, 'Icon', 'Warning');
if strcmpi(quit_choice, 'Exit this app')
app.delete;
return;
else
MFC_app_dir = fileparts(which('var.m')) % Check if the path has been updated
end
end
I manually add the file to the path, but the while loop does not exit. MFC_app_dir still shows up as empty even though the path has been updated. I'd appreciate some guidance on what silly mistake I'm making, TIA.
Joe
0 件のコメント
回答 (2 件)
Les Beckham
2023 年 2 月 14 日
I would actually expect that your while loop wouldn't execute at all, since var.m is a standard Matlab command for calculating variance.
MFC_app_dir = fileparts(which('var.m'))
while length(MFC_app_dir) == 0
disp('Entered the while loop')
end
See? It didn't enter the loop.
I would suggest using a different filename.
Also fileparts is unnecessary. It gives almost the same result as which by itself.
which('var.m')
I would probably use exist instead and force it to look in the current folder (or wherever the file is supposed to be).
Example
if exist(fullfile(pwd, 'filename.m'), 'file')
disp('found the file')
else
% display your uiconfirm dialog
end
6 件のコメント
Les Beckham
2023 年 2 月 14 日
I think what you need to do is use exist to see if the file exists and then, if it does, use your original approach to find out where it is.
filefound = (exist('xyzzy.m', file) == 2);
while ~filefound
quit_choice = uiconfirm(app.GPSDB_Simulink, 'xyzzy.m not found in Matlab Path!', 'Exit Dialog', ...
'Options',{'Fixed, continue', 'Exit this app'}, ...
'DefaultOption',1, 'Icon', 'Warning');
if strcmpi(quit_choice, 'Exit this app') % Selecting the Exit button closes the app
app.delete;
return;
else
filefound = (exist('xyzzy.m', file) == 2);
end
end
% Now we have found the file; where is it?
MFC_app_dir = fileparts(which('xyzzy.m'))
Prabhakar Vallury
2023 年 2 月 14 日
3 件のコメント
Les Beckham
2023 年 2 月 14 日
編集済み: Les Beckham
2023 年 2 月 14 日
I don't see that you are needing to do anything while that dialog is displayed except wait for the user to copy the file (presumably using the OS), or select Exit.
That seems like an odd user interaction to me, but I don't see why it wouldn't work.
参考
カテゴリ
Help Center および File Exchange で Downloads についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!