Create variable from string

7 vues (au cours des 30 derniers jours)
Teresa
Teresa le 18 Juin 2015
Modifié(e) : Stephen23 le 19 Juin 2019
I would like to alter a string in order to access a certain cell of a Matrix. Can I do that by creating a string first? For example
s='{2,1}{1,1}' and then somehow get A{2,1}{1,1}
Actually I would like to create something like a reference. Like
x=A{2,1} x=x{1,1} x=5
and then at the same time change A so I get the following value when calling
>>A{2,1}{1,1}
A{2,1}{1,1} =
5
Usually I would think of using a pointer but that does not seem to be possible in Matlab, or am I mistaken?
  1 commentaire
Stephen23
Stephen23 le 18 Juin 2015
Modifié(e) : Stephen23 le 18 Juin 2015
Avoid doing this.
MATLAB does not support pointers, and creating some hack-code to improvise them will not be robust.
In particular using dynamic strings like this to define indexing (or variables names) is liable to difficult-to-debug errors. Learn to program using more robust techniques, and your own life will become much easier...

Connectez-vous pour commenter.

Réponses (2)

Stephen23
Stephen23 le 18 Juin 2015
Modifié(e) : Stephen23 le 19 Juin 2019

Walter Roberson
Walter Roberson le 18 Juin 2015
eval(['A', s])
and for assignment
eval(['A', s, ' = ', Value_As_string])
Not recommended at all.
  1 commentaire
Stephen23
Stephen23 le 18 Juin 2015
Modifié(e) : Stephen23 le 19 Juin 2019
@Teresa: note that those words "Not recommended at all" that Walter Roberson wrote are actually very significant.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by