How to access workspace variables one by one using m-script

Hello,
I would like to read all workspace variables into array and check whether the name of variable is Matlab keyword or not. For example, I have four variables in workspace(a = 1;b=2;c=3;d=4;). I want to check if the varaible is keyword or not using matlab inbuilt function(iskeyword()) like below.
iskeyword('a')
ans =
0
>> iskeyword('if')
ans =
1
Is it possible check all base work space variables like this?

 채택된 답변

Nobel Mondal
Nobel Mondal 2015년 6월 1일
You can get all the workspace variables like this:
>> varList = evalin('base', 'who');
Then check individual parameters:
>> iskeyword(varList{k}) % for k-th parameter

댓글 수: 3

Hello Nobel,
I tried your code syntax also:
a = 1;
b=2;
c=3;
d=4;
y = 'if';
varList = evalin('base','who');
for k=1 : length(varList)
result_1 = iskeyword(varList{k}) % for k-th parameter
end
when I run this code, I am getting empty varList. Is that correct ?
Ah, you're probably calling this from inside a function. You need to define the WorkSpace context: 'base' or 'caller'
Or else, you simply can try
>> varList = who;
If the scope isn't changing.
N/A
N/A 2015년 6월 1일
편집: N/A 2015년 6월 15일
Hello Nobel,
after small change for your syntax.I got what I need.
allBaseWorkspaceVar = evalin('base','whos');

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2015년 6월 1일
a = 1;
b=2;
c=3;
d=4;
s1=whos
out=cellfun(@iskeyword,{s1.name})

댓글 수: 4

Hello Azzi,
thanks for reply..I tried your code:
a = 1;
b=2;
c=3;
d=4;
y = 'if';
s1=whos;
out=cellfun(@iskeyword,{s1.name})
output :
out =
0 0 0 0 0
It should returns last element '1'. could you please let me know why its not giving expected result?
in your example, the name of your variable is 'y', not 'if'
but when I type command "iskeyword(y)" , Matlab returns value '1'
what is that mean?
Maybe you need this
s1=whos
s2={s1.name}
for k=1:numel(s1)
out(k)=iskeyword(eval(s2{k}))
end

댓글을 달려면 로그인하십시오.

카테고리

제품

질문:

N/A
2015년 6월 1일

편집:

N/A
2015년 6월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by