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

4 visualizzazioni (ultimi 30 giorni)
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 Commenti
Stephen23
Stephen23 il 3 Lug 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.

Accedi per commentare.

Risposte (2)

KSSV
KSSV il 3 Lug 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 il 3 Lug 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};

Categorie

Scopri di più su Startup and Shutdown in Help Center e File Exchange

Tag

Prodotti


Release

R2013b

Community Treasure Hunt

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

Start Hunting!

Translated by