How do I use the who command within a function
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I am trying to use the who command within a function to make a list of the variables in the current workspace that match a given string.
Here is my little test function to get this portion working. In my case there are 6 variables that match the string so test should return "6". However, I always get 0. If I comment out the function line and run it as a script, it works fine. Is there a way to get the who command within a function to generate a list of variables from the current workspace?
function[test] = myfun()
%dev = 'R_A_bot';
%keys = strcat('IV','*',dev,'*');
vars = who ('IV*R_A_bot*');
test = length(vars);
Thank you.
0 Kommentare
Akzeptierte Antwort
Matt Fig
am 7 Sep. 2012
Bearbeitet: Matt Fig
am 7 Sep. 2012
When a function is running, the current workspace is the workspace of the function. That is just how it works.
Why not just keep it as a script?
If you must have a function for some reason, you can use this line, with either 'caller' or 'base' depending.
vars = evalin('caller','who (''IV*R_A_bot*'')');
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Whos finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!