Using 'while' loop to check if a file has been added to path

8 次查看(过去 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

回答(2 个)

Les Beckham
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'))
MFC_app_dir = '/MATLAB/toolbox/matlab/datafun'
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')
/MATLAB/toolbox/matlab/datafun/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 个评论
Prabhakar Vallury
Prabhakar Vallury 2023-2-14
I modified the code as below and it still doesn't work. i.e., does not leave the while loop. In the command window, I'm also manually tried these 2:
1) Added xyzzy.m to the path. exist('xyzzy.m') returns 2
2) Removed xyzz.m from the path. exist('xyzzy.m') returns 0
MFC_check_path = exist('xyzzy.m');
while MFC_check_path == 0
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
MFC_check_path = exist('xyzzy.m') % This returns 0 even after file is added to the path
end
end
Les Beckham
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
Prabhakar Vallury 2023-2-14
OK, I figured it out. 'uiconfirm' is a modal dialog, so all other actions are disabled until it is closed. Further, I cannot find a way to get the handle of this dialog to close it programmatically.
Seems Matlab doesn't have non-modal dialogs that come with multiple options - except 'uigetpref' which has a 'Do not show again' checkbox that I don't like at all. So I'll have to rethink my whole approach. Thanks for the insights and participation!
  3 个评论
Les Beckham
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.
Prabhakar Vallury
Prabhakar Vallury 2023-2-14
What I'm trying is allow the user to add the file to the Matlab path. But it appears the path is not refreshing while the uiconfirm dialog box (which is modal) is open. I can also confirm the command window is not active. When I had the dialog open, typing 'a = 1' did not return anything It did after closing the dialog. Puzzling behaviour, this seemingly little thing is taking forever. :)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Downloads 的更多信息

标签

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by