在Matlab的'for'循环的'to'中使用变量

阿比舍克·波尼斯(Abhishek Potnis)

forMatlab中循环的一般语法为

  for j = 1 to 5
    %body
  end

我想拥有一个变量,该变量的值是在运行时计算的,具体取决于用于控制for循环运行次数的用户输入

所以我想拥有:

m = input('Some Message');
n = ....% Some Calculations using user input 'm'
for j = 1 to n
  %body
end

我尝试了此操作,但出现错误-

Undefined function 'to' for input arguments of type 'char'.

Matlab正在考虑将变量n视为字符而不是变量。

PS:我是Matlab的新手,我在网上进行了很多搜索,但无法找到解决方案。请帮忙。

诺娜

实际上,您是否尝试过以下语法:

for j=1:n
  %body
end

我在文档中看不到“ to”一词因此,您上面的for循环语法将不是有效的Matlab语法。要考虑的另一件事是,n被解释为字符而不是数字。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章