How can I list global variables in MATLAB? - matlab

How can I see a list of what global variables are defined in MATLAB? (I am using R2009a).
I have hunted unfruitfully for this on Google and SO, so apologies if it has been asked before.

The WHO/WHOS commands can show you just the global variables:
who global %# Shows just the variable names
whos global %# Shows variable information, like size, class, etc.
You can also get the variable names/information returned in a variable instead of displayed on the screen:
names = who('global'); %# A cell array of variable names
data = whos('global'); %# A structure array of variable information

If you type whos at the command line Matlab will list all currently defined variables in your workspace. The last column of output is headed 'Attributes', global variables have the attribute 'global'.

Related

IPython/Spyder - how to list workspace variables with wildcard characters like MATLAB

I would like to list the workspace variables like MATLAB. For example, in MATLAB I can use "whos con*" to list all variables which start with "con". When I try the same thing in spyder, I get the following error - any workarounds?
whos con*
No variables match your requested type.

how do I load a list of variables from dot mat file

Suppose I have a big test.mat file, and I only wanted to load in several variables from it with variable names stored as varnames = {'var1','var10','var100'}. How would I transform varnames to be used in load?
Manually writing load('test.mat', 'var1','var10','var100') is not a good option since varnames could be long and dynamically generated. I also make it explicitly that individual variable names could have different lengths that char(varnames will fail.
load('test.mat', varnames) or load('test.mat', char(varnames)) or several other variants I tried do not work.
thanks.

Clearing a specific global variable in octave

I am trying to load the variables from a multiple .mat files using 'who' function and saving it in a variable 'A'. I am using a for loop for that. When I finish loading the first file and start loading the second file then 'A' shows variables in first .mat file as well. The problem is the function 'who' saves the variables as it is for multiple loops and I want to clear the 'who' after each loop. How can I do this. There is any way to clear a specific global variable.
for i=1:10; (10 mat files)
clear A;
clear who;
A=who; (all the variables in each mat file saved in A)
max(A(1,1); (finding max of variable A(1,1))
end
from the above code, if each .mat file has 5 variables then in the second loop the 'who' has 10 variables. the who is not cleared.
It's not entirely clear what you're trying to do because who (with no input arguments) returns a list of all variables in the current workspace not the variables within a file. For it to return a list of variables within a file you'd need to do something like:
vars = who('-file', filenames{i});
That being said, it looks like you actually want to load the variable A from all mat files that you've saved and find the maximum value of A across these files.
The better way to approach this is to specify an output to load which will load the data into a struct where each variable is stored as a separate field in the struct. You can also specify an additional input to load to specify that you'd only like to load variable A (in case there are other variables). You can then load each matfile into a separate struct and do your comparison
for k = 1:numel(filenames)
% Load variable A from this file into a struct
data(k) = load(filenames{k}, 'A');
end
% Now find the maximum value of A
maxA = max([data.A]);

Matlab - Saving multiple variables from workspace in separate text files

How do I save multiple variables from workspace in separate text files with file name as the corresponding variable name?
You could use something like this:
vars = who;
for k = 1:numel(vars)
save(vars{k},vars{k});
end
The who function grabs all of the variables in your current workspace and, in practice, could be replaced by a cell array of strings of just the variable names that you would like to save.

load matlab data file

I would like to set in a file all constant,parameters and then I can access from another class to any variable
for example const file const.m
FILE='file';
EDIT='edit';
COLOR_RED ='red',
COLOR_BLUE ='Blue'
START ='Start'
.....
...
and from any other files if I would like to access to any variable :
a = const.EDIT
so that I haven't to set a ='file' in each file.
How could I do that with matlab ?
how could also use enumeration ?
You don't need a struct just use the save command: http://www.mathworks.com/help/techdoc/ref/save.html and specify which workspace variables you want to save. If there are small like in your example, they will be easy to read in notepad for access not in matlab. In matlab just use the load function.
For example create two variable in matlab:
Var1 = 'Red'
Var2 = [1 2; 3 4]
now type
save('myData.mat');
this should create a file called myData.mat in your current working folder.
Now in another Matlab script you want to set a variable to have the value you saved as Var1 so you can go
NewVar = load('myData.mat', Var1)
And you can easily edit the values in the .mat file in a text editor such as notepad or gedit (or emacs or ms word or whatever) if you add the -ascii parameter when saving.
But you can also rewrite a variable by using save() again and specifying the variable name.