Is there any way to access a variable whose name is expressed by another string variable?

Is there any way in MATLAB to access a variable whose name is expressed by another string variable?
for example, when
myvar = 10;
string_myvar = "myvar";
were in the workspace and substituting the numerical scalar myvar into another myvar2 as
myvar2 = myvar;
Id’ like to find a way to give the right hand side "as the content of string scalar ‘string_myvar’ ".

7 件のコメント

KSSV
KSSV 2021 年 8 月 2 日
編集済み: KSSV 2021 年 8 月 2 日
Why it is needed? You can striaght away initialize
string_myvar = 10 ;
Or you may go ahead with the name
myvar = 10 ;
as it is.
Stephen23
Stephen23 2021 年 8 月 2 日
編集済み: Stephen23 2021 年 8 月 2 日
"Is there any way in MATLAB to access a variable whose name is expressed by another string variable?"
Yes, there are ways to do this.
But only if you want to force yourself into writing slow, inefficient, obfuscated, complex, buggy code that is hard to debug:
Daiki
Daiki 2021 年 8 月 2 日
@Stephen Cobeldick: the alternative ways in your link are what I wanted. thank you!
Stephen23
Stephen23 2021 年 8 月 2 日
@Daiki Tashiro: my pleasure! If you want more help/explanation/support, there are plenty of experienced volunteers on this forum who can help with those "alternative ways"... just ask!
Daiki
Daiki 2021 年 8 月 5 日
編集済み: Daiki 2021 年 8 月 5 日
I have a further question. In an originally defined function
function varargout = myfun(varargin)
a vector which defines a set of output variable and their order for varargout as
myargout = ["arg1", "arg2", "arg3"];
With this expression, I intend varargout{1} is for arg1. After some calculation, I substitute function output as
varargout{find(strcmp("arg1",myargout))} = arg1;
varargout{find(strcmp("arg2",myargout))} = arg2;
varargout{find(strcmp("arg3",myargout))} = arg3;
but in this way, I need to modify this substitution part every time the contents of myargout is changed, because (of course) the string "arg1" and a variable name arg1 are not related. For instance, if I change its contents to
myargout = ["arg1", "arg3", "arg4"];
(from "arg2" to "arg3" for the second variable of varargout, and from "arg3" to "arg4" for the third), then I need to change the substitution part as
varargout{find(strcmp("arg1",myargout))} = arg1;
varargout{find(strcmp("arg3",myargout))} = arg3;
varargout{find(strcmp("arg4",myargout))} = arg4;
where statement for arg2 is deleted and arg4 is added. What I want to do is automatize this substitution part, by only changing myargout.
Is there a good way for this?
I thought indexing was difficult to apply.
Stephen23
Stephen23 2021 年 8 月 5 日
編集済み: Stephen23 2021 年 8 月 5 日
Get rid of FIND: logical indexing is simpler.
The robust and efficient approach would be to put all of the variables into one container array (e.g. structure, cell array, table) and then use ISMEMBER or dynamic fieldnames or something similar to select the ones for the output.
For example:
C = {arg1,arg2,..,argN};
N = {'x1','x2',..,'xN'};
userwants = ["x1","x3"];
[X,Y] = ismember(userwants,N);
assert(all(X),'no match')
varargout = C(Y)
Daiki
Daiki 2021 年 8 月 5 日
@Stephen Cobeldick Thank you for the good approach, and it works well !

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

回答 (1 件)

myvar = 10;
string_myvar = "myvar2";
assignin('base', string_myvar, myvar);
myvar2
myvar2 = 10

2 件のコメント

Stephen23
Stephen23 2021 年 8 月 5 日
Or we could show beginners how easy it is to write simpler, neater, more robust, more efficient code:
darova
darova 2021 年 8 月 5 日
:D

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

カテゴリ

ヘルプ センター および File ExchangeScope Variables and Generate Names についてさらに検索

製品

リリース

R2021a

質問済み:

2021 年 8 月 2 日

コメント済み:

2021 年 8 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by