I want to write a parfor loop with nested variables. First folder name goes from 0 to 80 and each of them include another folder like 1, 2 ,3...11. but there is an error like this: "Assignment to x not supported. Variable has the same name as a local function." Is there any one can help me, please?
I have a code shown in below:
basedir=pwd;
filename='Analysis_GM.tcl';
exename='OpenSees.exe';
cmd=sprintf('"%s" < "%s"', exename, filename);
x=0:10:80
parfor i=1:length(x)
x(i)
end
function x(i)
parfor j=1:11
cd(fullfile(basedir, sprintf('%d',j)));
system(cmd);
end
end

 採用された回答

Jan
Jan 2019 年 1 月 16 日
編集済み: Jan 2019 年 1 月 16 日

1 投票

The error message is clear: You use "x" as a variable and a function. Simply use different names.
By the way: It is a very bad idea to use cd inside a parfor loop. Remember that the current folder is set globally, such that the different threads will compete. Better use absolute file names:
filename = 'Analysis_GM.tcl';
exename = 'OpenSees.exe';
cmd = sprintf('"%s" < "%s"', exename, filename);
parfor j=1:11
system(fullfile(basedir, sprintf('%d',j), cmd));
end

2 件のコメント

Esengul Cavdar
Esengul Cavdar 2019 年 1 月 16 日
編集済み: Jan 2019 年 1 月 16 日
Finally I have changed the code like this
y=50:10:80
parfor i=1:length(y)
x(i)
end
function x(i)
basedir=pwd;
filename = 'Analysis_GM.tcl';
exename = 'OpenSees.exe';
cmd = sprintf('"%s" < "%s"', exename, filename);
parfor j=1:11
system(fullfile(basedir, sprintf('%d',j), cmd));
end
end
But now I have taken an error that "system cannot find the file specified." I have controlled the file names there is no mistakes.
Jan
Jan 2019 年 1 月 16 日
I've edited your code to improve the readability. Please use the layout tools provided in this forum.
If the message tells you, that the specified file is not found, this file does not exist. If you controlled, that they do exist, the method to control the must be flawed.
I recomment not to rely on the current directory by using pwd or cd, because any callback of a timer, GUI or another thread can change this. Use full path names in every case. I assume this is the problem in your case.
basePath = 'C:\The\Folder\ToYour\Executable';
exePath = fullfile(basePath, 'OpenSees.exe');
cmd = sprintf('"%s" < "%s"', exePath, filename);

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Parallel for-Loops (parfor) についてさらに検索

質問済み:

2019 年 1 月 16 日

コメント済み:

Jan
2019 年 1 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by