- If it is impossible using writematrix, is there any way to save data to a spreadsheet format?
Issue with writematrix and parfor
3 次查看(过去 30 天)
显示 更早的评论
Hello,
I am currently running a 'parfor' loop with writematrix function in it. The writematrix writes some data to multiple sheets of an .xlsx file (each sheet corresponding to data of one particular run). While doing so, I get the following error:
Unable to write to file 'abc.xls'. Ensure the file is a valid spreadsheet file and is not password protected.
My file is a valid file and is not password protected.
However, if I run a simple 'for' loop, I have no problem and the whole code runs smoothly. My question is two parts 1) How do I use writematrix with parfor such that it does not give the above error? 2) If it is impossible using writematrix, is there any way to save data to a spreadsheet format?
I am desperate for answers :-(
Thanks
1 个评论
darova
2020-3-27
回答(1 个)
Edric Ellis
2020-3-30
You cannot write to the same file simultaneously from mulitple processes. (This is not a limitation specific to parfor - rather, it's a general limitation). To run in parallel, you must ensure that each worker is writing to a separate file. There are two ways you could do this:
- Base the file name on the loop iteration
- Base the file name on the "ID" of the task executing on the worker.
Here's a simple (untested) example:
parfor idx = 1:N
% Either: base file name on loop iteration
idxFilename = sprintf('outputFile_%d.xls', idx);
% Or: base file name on tassk ID
t = getCurrentTask();
taskIdFilename = sprintf('outputFile_%d.xls', t.ID);
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!