i have a variable in my workspace and i dont know its name directly

16 vues (au cours des 30 derniers jours)
Sajid Afaque
Sajid Afaque le 3 Juil 2020
for example i have loaded a variable
Verification_2018 = [ ];
in my workspace. now i directly dont know its name. i know that starting part would be "verification " but the year may change which i can get from another struct.
i.e struct.filename contains the name 'Verification_2018'
now using this name i am having trouble accessing the variable loaded in the workspace.
how can we do that
thanks in advance
  2 commentaires
Stephen23
Stephen23 le 3 Juil 2020
"now using this name i am having trouble accessing the variable loaded in the workspace"
Forcing meta-data (e.g. the year) into variable names is a sign that you are doing something wrong.
Rather than forcing yourself into writing slow, complex, buggy, difficult-to-debug code, you should load the file data into an output variable, and then you can avoid this situation entirely.

Connectez-vous pour commenter.

Réponses (2)

KSSV
KSSV le 3 Juil 2020
If you have variables in the workspace....to get the variable names use:
S = whose ;
names = {S.name} ;
Use strcmp to find whether your variable is present or not. Read about strfind as well.

Stephen23
Stephen23 le 3 Juil 2020
Do not load directly into the workspace, always load into an output variable (which is a scalar structure):
S = load(...);
Then you can efficiently access all of its fields e.g. using fieldnames and dynamic fieldnames:
If there is exactly one variable in the mat file (but you don't know its name) then you can simply access it like this:
C = struct2cell(S);
A = C{1};

Catégories

En savoir plus sur Variables dans Help Center et File Exchange

Tags

Produits


Version

R2013b

Community Treasure Hunt

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

Start Hunting!

Translated by