Effacer les filtres
Effacer les filtres

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

12 vues (au cours des 30 derniers jours)
Daiki
Daiki le 2 Août 2021
Commenté : darova le 5 Août 2021
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 commentaires
Stephen23
Stephen23 le 5 Août 2021
Modifié(e) : Stephen23 le 5 Août 2021
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 le 5 Août 2021
@Stephen Cobeldick Thank you for the good approach, and it works well !

Connectez-vous pour commenter.

Réponses (1)

Chunru
Chunru le 5 Août 2021
myvar = 10;
string_myvar = "myvar2";
assignin('base', string_myvar, myvar);
myvar2
myvar2 = 10

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by