从MATLAB中的其他变量创建变量:带复制的代码 [英] Creating variable from other variables in MATLAB: code with reproduction
问题描述
我对这种类型的代码有疑问:
I have a question regarding this type of code:
First = ["A","B","C"];
Second = ["D","E","F"];
Group = ["First", "Second"];
for gr = Group
current = gr;
for number = 1:numel(current)
my_variable(number) = current(number);
end
end
那是我的问题的再现.例如,在这种情况下,my_variable
等于"First"
.但是我希望它是变量"First"
中的"A"
然后是"B"
然后是"C"
.
That's the reproduction of my problem. In this case my_variable
is equal to "First"
for example. But I wanted it to be "A"
then "B"
then "C"
from variable named "First"
.
据我了解,此代码应执行以下操作:
For my understanding this code should do the following:
- 第一步:
for gr = Group
表示gr =第一到第二 - 第二步:
current = gr;
表示电流=第一步 - 第三步:
for number = 1:numel(current)
表示数字= 1:3(第一"中的元素数) - 第四步:
my_variable(number) = current(number);
表示my_variable = First(1)="A"
- 1st step:
for gr = Group
means gr = First to Second - 2nd step:
current = gr;
means current = First - 3rd step:
for number = 1:numel(current)
means number = 1:3 (number of elements in "First") - 4th step:
my_variable(number) = current(number);
means my_variable = First(1) = "A"
相反,我得到的my variable
等于"First"
或"Second"
.
Instead of that I get my variable
equal to "First"
or "Second"
.
希望您能理解我的意思.
I hope you understand what I mean.
P.S. 字符串数组我用双引号(" "
)(First = ["A","B","C"];
)仅在Matlab 2016b或更高版本中可用.
P.S. the string arrays I'm using with double quotes (" "
) like First = ["A","B","C"];
are only available in Matlab 2016b or later.
推荐答案
您应使用对于first
和second
,您还可以使用字符串数组:
For first
and second
, you can also use the string arrays:
first = ["A","B","C"];
second = ["D","E","F"];
group = {first, second};
for group_ind = 1:numel(group)
current = group{group_ind};
my_variable = strings(1,numel(current));
for number = 1:numel(current)
my_variable(number) = current(number);
disp(my_variable)
end
end
这篇关于从MATLAB中的其他变量创建变量:带复制的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!