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

조회 수: 1 (최근 30일)
Prabhakar Vallury
Prabhakar Vallury 2023년 2월 14일
댓글: Prabhakar Vallury 2023년 2월 14일
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에서 Interactive Control and Callbacks에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by