Can a system() Command be used Inside a Function Called from Inside a parfor Loop?
2 次查看(过去 30 天)
显示 更早的评论
Is the following code allowed:
result = zeros(100,1);
parfor ii = 1:100
% do something
result(ii) = myfun(resultfromdoingsomething)
end
function result = myfun(inp)
% do something and write a file based on inp
status = system(command); % command executes an executable that reads in the file that was written and produces an output file
result = % read in the file produced by the executable and grab a value to return
% delete the files
end
Even if allowed, are there any risks to be concerned about?
Would also like to know if there are any differences between 2024a and 2019b in this regard.
0 个评论
采纳的回答
Epsilon
2024-9-13
Hi Paul,
A 'system' command inside a function called from within ‘parfor’ seems to be working fine.
Code:
function myFunction(index)
command = sprintf('echo Processing index %d', index);
[~, cmdout] = system(command);
disp(['Output for index ', num2str(index), ': ', cmdout]);
end
parfor i = 1:100
myFunction(i);
end
One of the issues can be when multiple iterations try to access shared resources and can create conflicts.
Both the ‘parfor’ and ‘system’ are supported by the releases R2024a and R2019b so it should work fine.
Links to the Documentations:
system(R2024a): https://www.mathworks.com/help/matlab/ref/system.html
0 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!