Within a script, can I close or redirect a Windows File Explorer window opened with 'winopen'?

조회 수: 2(최근 30일)
Jeff Miller
Jeff Miller 2022년 5월 19일
답변: Paul 2022년 5월 21일
I have a script that processes iteratively through a number of directories, spending a few hours per directory. It would be very convenient to be able to view the directory currently being processed at each moment while the script is running.
MATLAB's 'winopen' command can be used to open a file explorer window at the start of processing each new directory, which is helpful. But these windows proliferate if the script issues a new 'winopen' each time it starts processing a new directory. With a lot of such windows open, it is tough to see which directory is currently being processed.
Hence my question: Is there a way to either:
- close an existing Windows Explorer window on a directory when I am done processing that directory, or
- point the existing Windows Explorer window to the new directory when the script changes directories?
Or maybe there is some entirely different approach?
Thanks for any suggestions,

채택된 답변

Jan 2022년 5월 19일
You could open a tree view to the folder inside Matlab:
[mtree, container] = uitree('v0', 'Root','C:\') % Still working?
This wa supported in R2018b, but maybe newer Matlab versions limit this.
  댓글 수: 3
Jeff Miller
Jeff Miller 2022년 5월 21일
Hi @Jan,
Thanks for your additional comment. It provided several useful hints, and I'm accepting your answer, even though it isn't quite the version that works best for me (so far), as shown below:
sTargetDir = 'C:\MATLAB'; % Change to any desired File Explorer target directory
% Start a new, separate, file explorer process, based on an answer from Walter Roberson at
% https://au.mathworks.com/matlabcentral/answers/418173-run-programm-in-background-without-using-start#answer_335999
proc = System.Diagnostics.Process();
proc.StartInfo.FileName = 'explorer.exe';
proc.StartInfo.Arguments = ['/n,/separate,' sTargetDir];
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
% Get list of active File Explorer processes using getProcess from FileExchange
procList1 = getProcess('explorer');
% Assume the new file explorer process just opened is the last one in this list
pid = procList1{end,1};
% kill that process. based on an answer from Walter Roberson at
% https://au.mathworks.com/matlabcentral/answers/500196-kill-a-processb-from-matlab
if ispc
cmd = sprintf('taskkill /PID %d > nul', pid); % > nul suppresses taskkill output string
cmd = sprintf('kill %d', pid);
s = system(cmd);
I'm not sure how robust this approach is, but it works in some simple cases that I've tried so far.

댓글을 달려면 로그인하십시오.

추가 답변(1개)

Paul 2022년 5월 21일
Maybe using an actxserver is a viable approach. I've used it for interacting with Power Point. It's not so bad once you get the hang of it and can find the good documentation on the methods, properties, objects, etc.
I did a quick search and found this link that seems to be on point and might be a good place to start. No idea what capabilities are actually available, but I'd be surprised if you can't make it do what you want.




Community Treasure Hunt

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

Start Hunting!

Translated by