Calling a variable from a variable

26 ビュー (過去 30 日間)
Matt
Matt 2011 年 6 月 13 日
e.g With variables:
a b c d
V = who;
Now:
V (2) = b
b(1) = 3000 (for example)
I would like to call variable b using the newly created variable V:
e.g something like [V(2)](1) = 3000
I'm not sure where to start tbh
any help would be great,
thanks in advance,
Matt

採用された回答

Matt
Matt 2011 年 6 月 13 日
Just done it using eval
e.g
a = eval((char(V(1,1)))); >> a (1,1);
thanks for the help guys,
Matt

その他の回答 (3 件)

Paulo Silva
Paulo Silva 2011 年 6 月 13 日
assignin('base',char(V(2)),3000)
  1 件のコメント
Matt
Matt 2011 年 6 月 13 日
Thanks, Instead of putting a value into the variable I would like to check the contents.

サインインしてコメントする。


Richard
Richard 2011 年 6 月 13 日
These kind of situations are best handled by storing the original data in a variable type that is designed to map strings to data. The simplest option is to use a structure which supports indexing with a string variable:
s.a = 1;
s.b = 2;
s.c = 3;
s.d = 4;
V = 'b';
s.(V) = 3000;
In some situations, you might want to look at the containers.Map object.

Matt Fig
Matt Fig 2011 年 6 月 13 日
I don't understand why you would want to do this, but here is a way to automate it.
clear all % Fresh start...
a = [1 2 3];
b = [4 5 6];
save myvars
N = who;
V = load('myvars')
V.(N{1})(3) % Returns a(3)

カテゴリ

Help Center および File ExchangeVariables についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by