Error in saving MAT file within a parfor!!!

1 view (last 30 days)
Hi
I am trying to use a parfor to speed up my loops. Within the loop I have to save('*.mat') and then use it in a function. When I use simple for loop, it works. When it goes through parfor, it returns the following error:
'Unable to write file *.mat: Invalid argument'
It is interesting that it works well on my laptop but not on PC!!! I have an academic license of MATLAB2013a. Besides, when the number of worker is between 1-4 it again works. But, in case of 5-8 workers it does not. It seems there is a problem with parfor.
Could you let me know what to do?!!!

Answers (1)

Walter Roberson
Walter Roberson on 13 Aug 2015
That error message indicates that you are literally trying to use the filename "*.mat" as the destination file name. You need to use a real file name.
Please show your code for creating the file name and for doing the save.
  4 Comments
Vahid Ghorbanian
Vahid Ghorbanian on 13 Aug 2015
This is the correct structure:
parfor i =1:n
data(i) = function(i);
function1(data(i)); % calculates some variables and save them
% as SimulinkParameters.mat in current
%directory
function2; % Calls SimulinkParameters.mat and runs a Model
end
Based on this code, it seems function1 is being called for every parfor iteration. right?
Walter Roberson
Walter Roberson on 13 Aug 2015
Yes. In a situation like that, where you are using the same filename for each worker, and in the same directory, then there is the possibility that two different workers are going to be trying to save() the file at the same time. As you are using MS Windows, the one that starts second is likely to fail because the file is locked for writing.
At the moment, I do not see that you are using the values stored in the .mat file, other than to build the list of variables to work on, which is something that could just be passed from function1 to function2.
I do see that you eval(List(i,1).name) which is eval() of the name of a variable stored in the .mat file. However, that eval() is not going to load the value from the .mat file: it is going to get the value from the current workspace if it is there and otherwise is going to look for the name as the name of a function to execute. Could I ask you to explain where you expect that the eval() will fetch the variable from?

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!