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

17 views (last 30 days)
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 Comments
Stephen23
Stephen23 on 3 Jul 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.

Sign in to comment.

Answers (2)

KSSV
KSSV on 3 Jul 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 on 3 Jul 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};

Categories

Find more on Variables in Help Center and File Exchange

Tags

Products


Release

R2013b

Community Treasure Hunt

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

Start Hunting!