Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
error appending to .mat file during parfor

Subject: error appending to .mat file during parfor

From: Mathew Thomas

Date: 21 Nov, 2012 22:58:08

Message: 1 of 2

Hello,

I am appending to an existing mat file using parfor loop. I am getting an error "Mat file could not be opened, file might be corrupt." I am guessing this is because the paralleled approach makes the cores to append/ or access the mat file at the same time.... Is there any work around for this ? Like being able to put a slight delay to the parfor loop etc.

Thanks in advance.

Mathew

Subject: error appending to .mat file during parfor

From: Edric M Ellis

Date: 22 Nov, 2012 10:13:15

Message: 2 of 2

"Mathew Thomas" <mathew99@gmail.com> writes:

> I am appending to an existing mat file using parfor loop. I am getting
> an error "Mat file could not be opened, file might be corrupt." I am
> guessing this is because the paralleled approach makes the cores to
> append/ or access the mat file at the same time.... Is there any work
> around for this ? Like being able to put a slight delay to the parfor
> loop etc.

Your best bet is to save to a unique file on each worker - there's no
way to synchronize the PARFOR workers to ensure they don't collide
accessing a single file. One option is to do it like this:

parfor ....
    t = getCurrentTask();
    myUniqueFilename = sprintf( 'baseFileName_%d.mat', t.ID );
    ...
    myFcnWhichSavesData( myUniqueFilename, ... ):
end

If you might run this with matlabpool closed, you might need to defend
against an empty return from getCurrentTask, like so:

    t = getCurrentTask();
    if isempty(t)
        id = 1; % might happen if MATLABPOOL is closed
    else
        id = t.ID;
    end

Cheers,

Edric.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us