Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Matlab中自动创建计数器变量_Matlab_Variables_Counter - Fatal编程技术网

如何在Matlab中自动创建计数器变量

如何在Matlab中自动创建计数器变量,matlab,variables,counter,Matlab,Variables,Counter,我正在设计一个应用程序,需要一开始就没有任何变量。但是,在某个时候,用户会输入他想要数的东西,比如说,桔子。然后他会输入“橙子”。在他写了oranges之后,我希望Matlab创建一个变量“orangesCounter”。有人知道我怎样才能做到这一点吗 或者可能有人知道我要做的事情的名字,如果它有一个特定的名字。我只是不知道如何正确地搜索这个问题,因为我不知道这种类型的变量创建是否有一个名称 谢谢 您将自己设置为一个巨大的混乱,因为您将如何处理代码中未知名称的变量?不管怎样,这是你的脚,这是枪:

我正在设计一个应用程序,需要一开始就没有任何变量。但是,在某个时候,用户会输入他想要数的东西,比如说,桔子。然后他会输入“橙子”。在他写了oranges之后,我希望Matlab创建一个变量“orangesCounter”。有人知道我怎样才能做到这一点吗

或者可能有人知道我要做的事情的名字,如果它有一个特定的名字。我只是不知道如何正确地搜索这个问题,因为我不知道这种类型的变量创建是否有一个名称


谢谢

您将自己设置为一个巨大的混乱,因为您将如何处理代码中未知名称的变量?不管怎样,这是你的脚,这是枪:

%# ask for input
varName = input('what do you want to count? Please enclose the word in single quotes.\n')

%# make the corresponding counter variable, initialize it to 0
eval(sprintf('%sCounter=0;'varName'));
这里有一个更好的方法:

variables = struct('name','','value','');

%# ask for the first variable
variables(1).name = input('what do you want to count? Please enclose the word in single quotes.\n');

%# ask for how many things there are
variables(1).value = input(sprintf('how many %s are there?\n',variables(1).name));

%# and return feedback
fprintf('There are %i %s.\n',variables(1).value,variables(1).name);

注意索引-在结构中可以有多个名称/值对。

我认为您正在寻找:在运行时创建变量。我看不出一个简单的方法来做到这一点。嘿,是的,在运行时创建变量听起来像一个合适的名字。谢谢我很难在谷歌上找到这个。哇,你的替代方案正是我所需要的。谢谢!