Error when accessing an Excel workbook from within a parallel loop

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Error when accessing an Excel workbook from within a parallel loop



I am using MATLAB to write to an Excel file with Macros in them, and then reevaluate the file and extract the Macro results using xlsread. The code works fine with only one worker. However, if I use parfor and use a second worker, MATLAB gives the error message as below. To be rigorous, I have created a second Excel file for the second worker and use t = getCurrentTask(); t.ID; to instruct the workers to work on their arranged excel file.


xlsread


parfor


t = getCurrentTask(); t.ID;



Error using xlswrite (line 226)



Invoke Error, Dispatch Exception:
Source: Microsoft Excel
Description: Microsoft Excel cannot access the file
'C:Users-----D0481000'. There are several
possible reasons:
• The file name or path does not exist.
• The file is being used by another program.
• The workbook you are trying to save has the same name as a currently open workbook.
Help File: xlmain11.chm
Help Context ID: 0



Error in extractFromExcelExpress (line 249)
xlswrite(filename,writtenIn,sheet,'B23');



This happens randomly. I did try to change the folder of the excel files and it starts to work for hours but ultimately stops. I did clear all the add-ins in Excel and I did put the corresponding folder to the excel trust locations.



The Excel version: 2016



The MATLAB version: R2017a





You're trying to open (and write to) the same file simultaneously from multiple workers... this will cause errors for the same reason that trying to do this manually would
– Wolfie
19 hours ago





Thank you Wolfie! However, I am sure I am using 't = getCurrentTask(); t.ID;' to make sure that each worker just works on the same file.
– Sheng
9 hours ago




1 Answer
1



This sounds like a typical race condition issue caused by multiple processes requiring exclusive access to the same file. The basic idea is that one process still hasn't closed the file when another tries to open it, and you only get errors sometimes because these issues don't always occur (see also: Heisenbug).



Here are several suggestions regarding what you can do (in decreasing order of what makes sense to me):


xlswrite


parfor


parfor


xlswrite1


Workbook.MultiUserEditing


xlswrite


while true, try, xlswrite(...); break; catch, pause(0.01); end, end





Thank you Dev-iL. For the first point, I think it won't work since the 'xlswrite' step is the main thing I want to accelerate with parfor so it has to be in the original way.
– Sheng
9 hours ago





For the second point, the fact that I need to reevaluate the form after writing to it will hinder this advice from working. I will try the third and come back. Thanks again!
– Sheng
8 hours ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

w,A ub0qGU hUuSYN,w6F5 w S01tFU m8C1TKCCEG U1sbXZ 1gOGM x876co1f7 4wM7sN 16 N lXR4,J7kyg i,I0H
YM7l,7T,bxP8aI4XIUWL8I12DDn8Lk,trCQ,iLCL1drcdxjGZRn4kP,OVZn

Popular posts from this blog

Makefile test if variable is not empty

Visual Studio Code: How to configure includePath for better IntelliSense results

Will Oldham