Is it possible to ADD a warning before Matlab starts?

So, I have the previously-reported issue that, on startup in Windows 10, Matlab reports that any folders that are on mapped network drives (Z:, Y: etc.) are "nonexistent or not a directory". I also lose any scripts or functions from those mapped folders that were open in the editor when Matlab was last shut down, so I have to find and re-open them.
The hack to get around this is to access the network drive(s) in Microsoft Windows Explorer/File Explorer, after which Matlab can see the folders in those drives - e.g. if functions or scripts were loaded into the editor, these will appear following the above hack.
What I'd like is a warning, before Matlab goes off and fails to find those mapped network drives, reminding me to open the offending drives in Windows Explorer before continuing.
Is there any way to do this via startup.m - or would it need a Windows batch file to incorporate the warning and the pause?

5 Commenti

It's a bit clunky, but I found some advice online to create a Windows batch file that sort of does what I want. It opens up a Windows dialogue box that has to be acknowledged before Matlab will start. Unfortunately it also opens up a command prompt window (that disappears once Matlab starts), so it's not very elegant... but still better than having to find half a dozen .m files that Matlab claims are "nonexistent"!
Copy the text below to an ASCII text file, and save it as e.g. NOT_MATLAB.BAT then add a shortcut to NOT_MATLAB.BAT wherever you wish (e.g. Desktop, pin to startbar etc.).
>NUL powershell "(new-object -COM WScript.Shell).popup('Open network drives if needed')"
"C:\Program Files\Matlab\R2023a\bin\matlab.exe"
dpb
dpb il 25 Lug 2024
Modificato: dpb il 25 Lug 2024
To reach the college network drives unless am logged into a college machine directly, I have to first connect through the VPN and map the drives; sounds as if you've got something slimilar going on. I have a batch file that does that which I run first when needing to be able to get to those drives. I haven't seen that it matters whether MATLAB is running or not; mapping the drives makes them visible after a session starts although, granted, the automatic reloading into the editor can't happen unless they are mapped at that time.
I've not trie tried it, but I suppose one could add a msgbox to the startup.m file to do the same thing as what the above batch file does; I do not know for certain about the sequence of starting up, however; whether the code to reload the editor will be before or after that little bit. Don't think it can hurt to try, however...
Thanks @dpb.
I tried the bat file suggestion in your comment. Unfortunately, trying to access the drives from the command prompt (i.e. in a batch file) before they have been "made available" in File Explorer just gives the error "The system cannot find the drive specified".
It's definitely a Windows issue rather than a Matlab one, as typing "NET USE" at the command prompt shows most of my mapped network drives are "Unavailable". It's odd because some of them map OK from the command prompt or from a batch file - others are stubbornly "Unavailable" until one has clicked on them in File Explorer. Bizarre.
It is possible to add "explorer Z:" to the batch file, and then Windoids will open up the mapped drive in a File Explorer window. Again, not super-elegant, but it works, and Matlab can then see folders on that drive.
"... the automatic reloading into the editor can't happen unless they are mapped at that time."
I haven't actually thought to test because I have the development environment on the local machine and only data files are on the shared drives, but I'd think if you simply typed edit after mapping the drives however that it would then load the previously open files again...unless it tries to be to clever and clears the past history of files that weren't available; that behavior I'm not sure about.
"...trying to access the drives from the command prompt (i.e. in a batch file) before they have been "made available" in File Explorer just gives the error "The system cannot find the drive specified"."
Yes, you have to check in the .cmd file -- here's the outline of one -- there are those needed and then a mapAll that calls them all.
@echo off
net use H: \\mapdrive\path password|orletprompt /USER:username /PERSISTENT:YES
if %errorlevel%==0 (exit /b)
net use H: /DELETE >nul
net use H: \\mapdrive\path password|orletprompt /USER:username /PERSISTENT:YES
It could probably be more elegant with powershell if one could ever figure out how to write it, but .cmd works. As you note, while one would think one could use
if not exist H: ...
but as noted, if the drive doesn't exist, it errors. Hence the attempt to map it first and then test the error condition and exit if it succeeds. This has been quite robust for my situation if not, as above, elegant.
I had not thought about the file explorer "trick" -- it doesn't appear a way to call it that doesn't open the window, however.

Accedi per commentare.

Risposte (1)

Image Analyst
Image Analyst il 25 Lug 2024
Modificato: Image Analyst il 25 Lug 2024
You can check for drives in your startup.m file, and warn you if the folder or drive is not there, like this
missingFolder = false;
folder = 'X:\';
if ~isfolder(folder)
warningMessage = sprintf('Warning: %s folder not found!', folder);
uiwait(warndlg(warningMessage));
missingFolder = true;
end
folder = 'Z:\my MATLAB files';
if ~isfolder(folder)
warningMessage = sprintf('Warning: %s folder not found!', folder);
uiwait(warndlg(warningMessage));
missingFolder = true;
end
if missingFolder
% Open File Explorer to the current folder if we didn't find one.
winopen(pwd);
end
If you have some top level folder where all your m-files live, even in subfolders, you can use the Setpath button on the tool ribbon to "Add with subfolders" the folder, then save the path. Or you can put this into your startup.m file:
folder = 'Z:\my MATLAB files';
addpath(genpath(folder));
savepath();

1 Commento

Nice. I'll try that tomorrow, but the suggestion looks promising.

Accedi per commentare.

Categorie

Prodotti

Release

R2023a

Richiesto:

il 25 Lug 2024

Commentato:

dpb
il 25 Lug 2024

Community Treasure Hunt

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

Start Hunting!

Translated by