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

4 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2020-7-3
"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.

请先登录,再进行评论。

回答(2 个)

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

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

产品


版本

R2013b

Community Treasure Hunt

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

Start Hunting!

Translated by