Bypass uigetdir in function
Show older comments
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 Comments
You are starting to discover the disadvantages of how beginners write code: repeatedly asking the user for input, cd to every directory, repeated code, uncontrollable scripts....
The best solution is to write better code, e.g.:
- write functions where any parameters are input arguments, then trivially define that folder once and pass it as an input argument to as many functions as you want.
- avoid slow and hard-to-debug cd by using efficient absolute/relative filenames.
Adam
on 18 Mar 2019
If you can't edit the code that does the thing you don't want then No, I can't think of a way to change its behaviour otherwise! You can play around with java.Robot to take control of mouse movements, but that is a vastly overblown solution.
Ideally you would simply edit the script, which should instead be a function rather than a script, too.
thibault
on 19 Mar 2019
Guillaume
on 19 Mar 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.
Accepted Answer
More Answers (0)
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!