Bypass uigetdir in function

2 visualizaciones (últimos 30 días)
thibault
thibault el 18 de Mzo. de 2019
Comentada: Guillaume el 19 de Mzo. de 2019
Hello everyone,
I am trying to run a script (i.e. script.m) inside my main script (i.e. main_script.m) and there is a uigetdir at the beginning of the script.m to select the folder in which it will work.
The problem is the following, I have to run script.m twice in my main_script.m and I do not want to choose the folder everytime. I also want to specify that I cannot change and/or modify any line in the script.m.
Here is an example of the main_script.m
% main_script.m
cd(dir1)
script % This is the script in which the uigetdir is
cd(dir2)
script % 2nd time but with a different directory
And an example for the script.m
% script.m
uigetdir
...
Is it possible to bypass the uigetdir function in a way that I do not have to click to select the folder ?
I noticed that if I use cd before starting the script, I just have to click "select folder" and it works. Is it possible, then, to automatically click on the "select folder" button without actually clicking ?
Thank you for your answers.
Sincerely
TM
  4 comentarios
thibault
thibault el 19 de Mzo. de 2019
Thank you for your answers.
I know the script should have been a function but this script has been the main function for many years and many people, and it is shared between all these people and me. This is why I cannot really modifiy this code...
Guillaume
Guillaume el 19 de Mzo. de 2019
"it is shared between all these people and me. This is why I cannot really modifiy this code..."
For me that's all the more reasons to actually modify it and make it better for everyone. You can bet that you're not the only one who's having to work around that badly designed interface.

Iniciar sesión para comentar.

Respuesta aceptada

Guillaume
Guillaume el 18 de Mzo. de 2019
The proper way to go about this is to separate the processing code into a function, completely independent of user input/output. That's what should have been done in the first place. Then you can have separation functions/scripts to deal with UI exclusively and the input can come from any sources you wish (uigetdir, complex UI, prompts, hard coded paths, database, config files, etc.)
That's really what you should do even though you said you don't want to modify the original code. In the long run, that's the only maintainable option. Anything else involves hacks that will break at some point.
The simplest way to hack this without editing the original code, is to have your local uigetdir function that shadows matlab one. Assuming the code does not cd, having an uigetdir in your code directory would do. Within that function, you can do what you want. E.g:
function selpath = uigetdir(varargin{1})
%This function completely ignores the inputs normally passed to uigetdir
%It also does not display a dialog. Instead it return the preset path
%to set the path, call the function with
% uigetdir(0, pathtoset)
persistent path;
if isempty(path)
path = userpath; %use default folder
end
if nargin > 1 && isnumeric(varargin{1}) && varargin{1} == 0
path = varargin{2};
end
selpath = path;
end
With that example above, you'd call the function in your script to set the path:
uigetdir(0, 'C:\somewhere\some\folder');
The sub-script would then get this folder back each time it calls uigetdir.
However, I must emphasise again that it is a hack that will most likely cause you problems one day (usually, just before an important deadline).
  1 comentario
thibault
thibault el 19 de Mzo. de 2019
Thank you for your answer.
I get that creating my own uigetdir function would be a hack so I won't try it. Thank you for the proposition.
I will mark that question as answered as I get that there is no simple solution to my problem. Thank you all for your help.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by