Effacer les filtres
Effacer les filtres

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

19 vues (au cours des 30 derniers jours)
gvreddy
gvreddy le 1 Juin 2015
Modifié(e) : gvreddy le 15 Juin 2015
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?

Réponse acceptée

Nobel Mondal
Nobel Mondal le 1 Juin 2015
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 commentaires
Nobel Mondal
Nobel Mondal le 1 Juin 2015
Modifié(e) : Nobel Mondal le 1 Juin 2015
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.
gvreddy
gvreddy le 1 Juin 2015
Modifié(e) : gvreddy le 15 Juin 2015
Hello Nobel,
after small change for your syntax.I got what I need.
allBaseWorkspaceVar = evalin('base','whos');

Connectez-vous pour commenter.

Plus de réponses (1)

Azzi Abdelmalek
Azzi Abdelmalek le 1 Juin 2015
a = 1;
b=2;
c=3;
d=4;
s1=whos
out=cellfun(@iskeyword,{s1.name})
  4 commentaires
gvreddy
gvreddy le 1 Juin 2015
but when I type command "iskeyword(y)" , Matlab returns value '1'
what is that mean?
Azzi Abdelmalek
Azzi Abdelmalek le 1 Juin 2015
Maybe you need this
s1=whos
s2={s1.name}
for k=1:numel(s1)
out(k)=iskeyword(eval(s2{k}))
end

Connectez-vous pour commenter.

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by