FliegenderZirkus
FliegenderZirkus

Reputation: 319

MATLAB: Running a .bat in parallel: multiple working directories possible?

I am running a .bat file using the command system (or dos) in a parfor loop in Matlab 2013a. Is there a way to change in which directory the command gets executed? So far it seems that it is always the current (working) directory. Another option would be to change the working directory inside the parfor loop, but that would mean having multiple working directories at the same time, which doesn't seem to be possible. The reason I do this is that I have one Simpack model and want to run several different simulations at the same time. In serial for-loop I can do this by copying the simulation specification to the Simpack folder one-at-a-time, but in parallel this must be changed.

parfor i=1:2
    ...
    cd(path_model_main_temp_i);
    system('C:\SIMPACK\SIMPACKv8.9\s_8904\simpack\com\spck.bat simpack integ modelname');
    copyfile(path_results_temp_i, path_results{i});
end

This snippet doesn't work, but is it the right direction?

EDIT: It turns out there was a different problem with my code. It is possible to have a cd command inside a parfor loop.

Upvotes: 1

Views: 783

Answers (2)

Mark Setchell
Mark Setchell

Reputation: 208107

Try changing directory within the process created by the system() command - which is fine as it is a new, and distinct process, it can have its own directory.

Something like this:

system('cd somewhere & C:\SIMPACK...');

Upvotes: 0

Edric
Edric

Reputation: 25160

I think you're on the right track here. One thing you could do use getCurrentTask to work out where to put that directory. For example, something like this:

parfor idx = 1:2
    t = getCurrentTask();
    if isempty(t)
       % running on the client - use tempdir
       d = tempdir();
    else
       % on a worker - make a subdirectory using task ID
       d = fullfile(tempdir(), num2str(t.ID));
       mkdir(d);
    end

    cd(d);
    pwd
    % do stuff
end

Upvotes: 0

Related Questions