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

조회 수: 17 (최근 30일)
Sajid Afaque
Sajid Afaque 2020년 7월 3일
답변: Stephen23 2020년 7월 3일
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에서 Variables에 대해 자세히 알아보기

태그

제품


릴리스

R2013b

Community Treasure Hunt

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

Start Hunting!

Translated by