Octave 在一个八度的for循环中一个接一个地执行所有情况

Octave 在一个八度的for循环中一个接一个地执行所有情况,octave,Octave,到目前为止,我都是手动更改req。代码可以工作,包括将结果保存到文件中。 但是现在我想为req的所有可能值运行代码 如果不将其保存到文件中,代码可以工作,但显然它会覆盖结果。 这就是为什么我把保存结果的那行代码放进去,根据req的值给它一个不同的名称。但这给了我错误 错误: 我的代码: clear all; clc; for req = {"del_1", "del_2", "del_3"} request = req; if (strcmp(request, "del_1"))

到目前为止,我都是手动更改
req
。代码可以工作,包括将结果保存到文件中。 但是现在我想为req的所有可能值运行代码

如果不将其保存到文件中,代码可以工作,但显然它会覆盖结果。 这就是为什么我把保存结果的那行代码放进去,根据
req
的值给它一个不同的名称。但这给了我错误

错误:

我的代码:

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={“delu 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)
我认为这样可以更清楚地了解您实际在做什么,代码就不那么复杂了


请注意,在八度音程中,
[“del_1”“del_2”“del_3”
的计算结果为
“del_1del_2del_3”
。也就是说,连接字符串。在MATLAB中,情况并非如此,但Octave不知道
字符串的类型,并且使用
相同的方法在循环中创建
字符数组。

for req = {"del_1", "del_2", "del_3"}
req
获取单元格数组的每个单元格的值,而不是单元格的内容(奇怪的设计决策,IMO,但这就是它的工作方式)。因此,
req={“delu 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)
我认为这样可以更清楚地了解您实际在做什么,代码就不那么复杂了



请注意,在倍频程中,
[“del_1”“del_2”“del_3”]
的计算结果为
“del_1del_2del_3”
。也就是说,将字符串连接起来。在MATLAB中,情况并非如此,但倍频程不知道
字符串的类型,并使用
使用与
相同的方法创建
字符
数组。

谢谢您的回复。问题是,
tarr
不是
req
每个成员的唯一变量。对不起,我以前不清楚。我在上面编辑我的帖子。所以,像您的建议一样将所有变量列为参数将是很困难的。@Codelearner777:很公平。不过,我希望答案的第一部分能解决你的问题。我只是回到这个问题上来。它起作用了<代码>请求=请求{1}
请求=req工作。但请求的内容似乎不被视为字符串。我检查了
isstring(req(1))
isstring(req)
isstring(request(1))
isstring(request)
,都给出了
0
。结果是我看错了你的代码,或者是我的代码输入错误<代码>请求=请求{1}
request=req{}用于循环和命名保存的文件。谢谢你的回复。问题是,
tarr
不是
req
每个成员的唯一变量。对不起,我以前不清楚。我在上面编辑我的帖子。所以,像您的建议一样将所有变量列为参数将是很困难的。@Codelearner777:很公平。不过,我希望答案的第一部分能解决你的问题。我只是回到这个问题上来。它起作用了<代码>请求=请求{1}
请求=req工作。但请求的内容似乎不被视为字符串。我检查了
isstring(req(1))
isstring(req)
isstring(request(1))
isstring(request)
,都给出了
0
。结果是我看错了你的代码,或者是我的代码输入错误<代码>请求=请求{1}
request=req{}用于循环和命名保存的文件。谢谢