在八度音程的for循环内一个接一个地执行所有情况
Executing all cases one after another inside a for loop in octave
到目前为止,我都是手动更改 req
。该代码有效,包括将结果保存到文件中。
但是现在我想 运行 req.
所有可能值的代码
在不将其保存到文件中的情况下,代码可以运行,但显然它会覆盖结果。
这就是为什么我放置那行代码,通过根据 req
的值给它一个不同的名称来保存结果。但这给了我错误。
错误:
error: sprintf: wrong type argument 'cell'
error: called from
testforloop at line 26 column 1
我的代码:
clear all;
clc;
for req = {"del_1", "del_2", "del_3"}
request = req;
if (strcmp(request, "del_1"))
tarr = 11;
# and a bunch of other variables
elseif (strcmp(request, "del_2"))
tarr = 22;
# and a bunch of other variables
elseif (strcmp(request, "del_3"))
tarr = 33;
# and a bunch of other variables
else
# do nothing
endif
#long calculation producing many variable including aa, bb, cc.
aa = 2 * tarr;
bb = 3 * tarr;
cc = 4 * tarr;
#collecting variables of interest: aa, bb, cc and save it to a file.
result_matrix = [aa bb cc];
dlmwrite (sprintf('file_result_%s.csv', request), result_matrix);
endfor
如果我使用["del_1" "del_2" "del_3"]
,错误是
error: 'tarr' undefined near line 20 column 10
error: called from
testforloop at line 20 column 4
循环内部
for req = {"del_1", "del_2", "del_3"}
req
获取单元格数组的每个单元格的值,而不是单元格的内容(奇怪的设计决定,IMO,但这是它的工作方式)。因此,req={"del_1"}
在第一次迭代中。然后可以使用 req{1}
获取字符串本身。所以你需要改变的是:
request = req{1};
但是,我会以不同的方式实现它,如下所示:
function myfunction(request, tarr)
% long calculation producing many variable including aa, bb, cc.
aa = 2 * tarr;
bb = 3 * tarr;
cc = 4 * tarr;
% collecting variables of interest: aa, bb, cc and save it to a file.
result_matrix = [aa bb cc];
dlmwrite (sprintf('file_result_%s.csv', request), result_matrix);
end
myfunction("del_1", 11)
myfunction("del_2", 22)
myfunction("del_3", 33)
我认为这样可以更清楚地了解您实际在做什么,代码不那么复杂。
请注意,在 Octave 中,["del_1" "del_2" "del_3"]
的计算结果为 "del_1del_2del_3"
。也就是说,您连接字符串。在 MATLAB 中情况并非如此,但 Octave 不知道 string
类型,并且使用 "
与 '
相同的方式来创建 char
数组。
到目前为止,我都是手动更改 req
。该代码有效,包括将结果保存到文件中。
但是现在我想 运行 req.
在不将其保存到文件中的情况下,代码可以运行,但显然它会覆盖结果。
这就是为什么我放置那行代码,通过根据 req
的值给它一个不同的名称来保存结果。但这给了我错误。
错误:
error: sprintf: wrong type argument 'cell' error: called from testforloop at line 26 column 1
我的代码:
clear all;
clc;
for req = {"del_1", "del_2", "del_3"}
request = req;
if (strcmp(request, "del_1"))
tarr = 11;
# and a bunch of other variables
elseif (strcmp(request, "del_2"))
tarr = 22;
# and a bunch of other variables
elseif (strcmp(request, "del_3"))
tarr = 33;
# and a bunch of other variables
else
# do nothing
endif
#long calculation producing many variable including aa, bb, cc.
aa = 2 * tarr;
bb = 3 * tarr;
cc = 4 * tarr;
#collecting variables of interest: aa, bb, cc and save it to a file.
result_matrix = [aa bb cc];
dlmwrite (sprintf('file_result_%s.csv', request), result_matrix);
endfor
如果我使用["del_1" "del_2" "del_3"]
,错误是
error: 'tarr' undefined near line 20 column 10
error: called from
testforloop at line 20 column 4
循环内部
for req = {"del_1", "del_2", "del_3"}
req
获取单元格数组的每个单元格的值,而不是单元格的内容(奇怪的设计决定,IMO,但这是它的工作方式)。因此,req={"del_1"}
在第一次迭代中。然后可以使用 req{1}
获取字符串本身。所以你需要改变的是:
request = req{1};
但是,我会以不同的方式实现它,如下所示:
function myfunction(request, tarr)
% long calculation producing many variable including aa, bb, cc.
aa = 2 * tarr;
bb = 3 * tarr;
cc = 4 * tarr;
% collecting variables of interest: aa, bb, cc and save it to a file.
result_matrix = [aa bb cc];
dlmwrite (sprintf('file_result_%s.csv', request), result_matrix);
end
myfunction("del_1", 11)
myfunction("del_2", 22)
myfunction("del_3", 33)
我认为这样可以更清楚地了解您实际在做什么,代码不那么复杂。
请注意,在 Octave 中,["del_1" "del_2" "del_3"]
的计算结果为 "del_1del_2del_3"
。也就是说,您连接字符串。在 MATLAB 中情况并非如此,但 Octave 不知道 string
类型,并且使用 "
与 '
相同的方式来创建 char
数组。