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!