what happens to Matlab when I lose connection to my network drive data all of a sudden?

12 次查看(过去 30 天)
Hi there
I have been trying to run some matlab on a bunch of my data. Both my codes and data are stored in the network drive. Recently due to unstable internet, the network drive connection also became unstable. As a result, matlab stops once in a while, saying that it is Unable to open file "xx" for reading. Is there a way to pause the matlab instead of stopping it completely? It seems that I have rerun the matlab everytime when this error comes and it is really frustrating that I just lost so many hours' work.
I would appreciate a lot if you can give me any advices.
Thanks

采纳的回答

YT
YT 2019-2-5
编辑:YT 2019-2-5
Well that sounds indeed frustrating. Luckily you can pause on error/warnings (check it here). I think thats a screenshot for an older version, but with R2018b its pretty similar.
p.s.
I know it's not really my business, but I would rather try to fix the internet connection (not only for Matlab but also for other things it's just annoying to get disconnected everytime).
  6 个评论
Shirley
Shirley 2019-2-5
Yeah it works now. And internet is back to normal as well. :)
Thank you so much again.
BTW, the error message won't pop out anymore once I use 'Pause on errors' right?
Guillaume
Guillaume 2019-2-5
The error still pops up if you pause on Error. Things to be aware if you do that:
  • when an error occurs, matlab enters debugging mode
  • That can be easily seen because the command prompt changes to K>> (instead of just >>)
  • In debugging mode, you're in the workspace of whichever function was executing. If it's a script, you're in the base workspace.
  • If you stay in debugging mode, any variable you create will be created in the current workspace. When you leave debugging mode, these variable will disappear with the function workspace
  • Do not execute other code while in debugging mode, certainly do not restart your current function the same way. If it errors again, you'd have to leave debugging mode twice.
  • To quit debugging mode either click on the red 'quit debugging button' or write dbquit. Once you're out of debugging mode, the prompt should revert to >>.
  • You cannot edit the code you're currently running while in debugging mode.
In my opinion, using this method as a way to pause execution to cope with network error is very much a hack.

请先登录,再进行评论。

更多回答(1 个)

Guillaume
Guillaume 2019-2-5
As YT said, fixing the problem may be a more efficient use of your time than trying to work around it. Assuming you have enough local disk space, creating a synced copy of the network folder (offline files in Windows, some other name in other OSes) may allow you to avoid having problem with disconnections.
Otherwise, yes, you can work around it. It's going to be a pain, particularly with the way matlab IO is designed. First, you won't get an actual error when you try to open a file that is unavailable. You just get an invalid file ID. It's only when you try to use that invalid file ID (e.g. in a fread call) that you get an error. And of course, since the connection can drop at any time, any read or write operation can fail while previous ones succeeded.
So, to limit the chance of failure and the number of locations where you have to deal with them, I recommend you make your IO code as simple as possible. Don't process anything. Wait until you've read everything you need. Then I would wrap that file IO code inside a try ... catch statement. If an error is caught during IO, simply retry the whole IO from the beginning.:
%... code before the IO. preallocates whatever is needed
filetoopen = '\somewhere\somefile';
while true %loop until we've read the whole file succesfully
try
%all file IO in here
fid = fopen(filetoopen);
fileheader = fread(fid, [1, 4], '*uint8'); %may fail
fseek(fid, 8, 'cof'); %may fail
filedata = fread(fid, [1, Inf], '*char'); %may also fail
fclose(fid); %can this fail?
%if we're here we've read the whole file, quit while loop
break;
end
%if we're here an error occured during IO, since we haven't broken out of the loop
if fid > -1
try, fclose(fid); end %still try to close the file. Ignore error, it's a lost cause
end
warning('failed to read "%s". Retrying', filetoopen);
%you may want to wait a bit before retrying
pause(10);
end %retry rereading the file
%... rest of the code. If we're there the file has been read succesfully.
  3 个评论
YT
YT 2019-2-5
+1 for the nice explanation and sample code (always love to see what the more experienced users come up with ;)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by