How to derive a variable name from a variable?
显示 更早的评论
How to derive a variable name from a variable?
A = [5 6 7 8];
B = ‘A’;
A(1) % this works and return the first element of A which is 5
B(1) % this doesn’t work like the code above
4 个评论
Stephen23
2017-2-7
What exactly are you trying to do? It doesn't sound like a good plan - dynamic variable names are a very bad idea.
A = [5 6 7 8];
B = A;
A(1);
B(1);
would give you what you want from the perspective of the information you have given, that you simply want B(1) to return the same as A(1).
Rightia Rollmann
2017-2-7
Adam
2017-2-8
If B is a field of A why are you trying to create a variable B that represents the whole of A.
Just A.( 'B' ) would give what you want. Fields are not really ordered in a struct, at least not for general usage.
采纳的回答
更多回答(2 个)
Star Strider
2017-2-7
MATLAB does not recognise char(0145) and char(0146) as quotation marks in code.
A = [5 6 7 8];
B = 'A' % Sets ‘B’ TO Be Character ‘A’
B = A
B(1)
produces:
B =
A
B =
5 6 7 8
ans =
5
A.B(:, 1:2)
类别
在 帮助中心 和 File Exchange 中查找有关 Dynamic System Models 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!