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

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.

Related

Latex variable automation: writing variables for Latex directly from Stata and MATLAB

I am working on this project which requires analyzing a large (>50GB) dataset in a server, both in Stata and MATLAB. Both parts are required and I cannot use only one of them.
My ultimate goal is to generate a .tex file named something like commands.tex which looks like this:
\newcommand{\var1}{val1}
\newcommand{\var2}{val2} % MATLAB file matlab_file.m on DD/MM/YYYY
\newcommand{\var3}{val3} % Stata file stata_file.m on DD/MM/YYYY
...
where variables are ordered alphabetically and each of the values is most probably a number. Note that the commands in the comments would help me trace where did I generate the values. The usage of the file is so that after a preamble I can use LaTeX on the following way:
<preamble>
\input{commands.tex}
\begin{document}
Variable 1 has a value of \var1 and variable 2 has a value of \var2.
\end{document}
The purpose of this is so that I can analyze locally (or remotely) a sample, say of 0.1 or 10 percent of the total observations, write a report with those, and then run the analysis again with a bigger size. I want to completely eliminate the chances of me copying a number wrong.
I am trying to write some code both in MATLAB and Stata, but I think that is beyond my expertise, and would be very grateful if someone could help me figure out how to do it. To be honest, I feel I would be able to do the MATLAB part but the Stata I have no idea.
Stata code
What I am trying to do is to generate a command that takes as an input a name and a scalar and as an output defines the corresponding variable in my commands.tex file detailed above. My goal is to be able to generate something like this:
sysuse auto
reg price weight
define_variable PriceWeight = _b[weight], format(%4.2f)
and what I hope the code to do is that:
If \newcommand{\PriceWeight} does not exist in commands.tex then it adds its value to the list, preserving the alphabetical order.
If the variable exists then it deletes its value and rewrites above it, with the value given in the scalar.
I know how to give the values to a program in Stata, but I do not exactly know how to use those values and perform the necessary commands. The syntax is something like:
program define define_variable
syntax anything = X, [format(string)]
<other code>
end
Note: Of course, I need something way deeper than regression coefficients, but as a simple example this would suffice.
MATLAB code
This seems to be easier in MATLAB, but I do not know exactly how to automate the process. In MATLAB what I want to be able to do is something like:
clc; clear;
PriceWeight = 3
define_variable('PriceWeight',PriceWeight,format)
again where it automatically goes to the single file and updates it accordingly. Any hel[p with be very much appreciated.
Based on your comments and assuming that your file with all relevant variables is not huge, I would suggest getting your data from Stata to Matlab, and update your variables there as necessary (using functions such as exist or strcmp if you have a list of names). A quick google search gives me this link for Stata to Matlab.
To make it easy to process you might want to create a cell (I will call C), where one column contains all variable names and one column contains the scalar values.
Then, once you have assembled all your variables, you can sort your cell alphabetically and write it to a file using this.. Of course you would write a .tex file, and then iterate over your cell with something like
fprintf(fID,'\newcommand{\%s}{%f} ',C{i,1},C{i,2})
I hope this is understandable and helps.

SPSS issue with RENAME VARIABLES

I have a long syntax (1800 lines) and this one portion has been giving me trouble. I can't for the life of me figure out what I'm doing incorrectly.
It is supposed to take an existing file and narrow it down to just the variables listed in the /KEEP statement. Then every variable is renamed to a similar variable name, but "oldxxxx". Later my syntax matches the new file to this updated variable file and points out any changes in the values, giving a list of reasons in the recoded file.
Once the syntax reaches the first RENAME VARIABLES I get the following error:
RENAME VARIABLES Duplicate variable names from RENAME.
Thank you in advance!
First a couple of remarks: It would be better practice to save to a different file name. In your syntax the original file gets saved over and you can't go back... Also I recommend you follow #Andy W's advice regarding how to keep only the variables you need in your file.
Now, in the sample syntax you posted I see an error - possibly that's your problem:
RENAME VARIABLES (total_EMFASYS_award=oldgrant).
The new name is oldgrant instead or oldtotal_EMFASYS_award. Possibly further down you've got another command saying
RENAME VARIABLES (grant=oldgrant).
hence the double name.
To avoid such errors and shorten your syntax, you could use the following macro:
define renVars (!pos=!cmdend)
rename variables
!do !i !in (!1) !i = !concat("old",!i)
!doend .
!enddefine.
After running this macro definition you can run the macro by stating the macro name and the full list of variables you want renamed, like this:
renVars
Student_ID rl_highschoolgpa comb need qualitygrp NewUpfrontGrant meritgrant
targetcounty_housing housinggrant tuitiongrant athlete_recruit .
One thing to note about RENAME VARIABLES command - it also works like this:
RENAME VARIABLES (list_of_starting_variable_names = list_of_final_variable_names).
you would just need to provide the 2 names lists, and the renaming will be done in th eorder in which the names are provided (1st variable in list 1 gets renamed to the 1st variable in list 2,... n-th variable in list 1 into the n-th variable in list 2... and so on.
This should avoid the Duplicate Variable Names error you are getting, as all renames are done in one go. But would require you to alter the original syntax a bit, and is a bit harder to spot which variable gets renamed into which variable.

Using matlabs save in functions

Is it possible to use the Matlab save command inside a function to store workspace variables?
Consider following scenario: I've got a bunch of variables in the Matlab workspace and want all that are beginning with "a" and "b" in a .mat file. Of course this works:
save('test.mat','a*','b*')
but i want to have a variable filename. The function i wrote:
function save_with_name(name)
save(name,'a*','b*')
does not work, because save_with_name doesn't see the workspace variables. Is there a solution which i can use?
You need to evaluate save in the base workspace.
function save_with_name(name)
expression = ['save(''', name, ''',''a*'',''b*'')'];
evalin('base',expression);
The double-quotes ('') in the expression are necessary to allow the quote character itself (').
Thus the command you're looking for is: evalin

How to make matlab see functions defined in .m files?

I'm a total newbie to MATLAB but I have to write some code in it. I've had problems with making MATLAB see functions I've defined in external .m files. This is what I've done: I've created a file named, say, foo.m in my home dir with the following contents:
function [y] = foo(x)
% description
y = x + 1
When I run matlab (my home dir is matlab's workdir) it does not see foo function - it replies with standard ??? Undefined function or variable 'foo' message. BUT help foo or which foo return correct data printing help text and pointing on foo.m file respectively.
I must be missing something but I have no idea what it is. This is getting very annoying.
Oh, after several trial and error attempts I've managed to call that function. Unfortunately I can't remember the sequence of steps I've performed. Moreover after restarting matlab it returns to its usual 'Undefined function or variable' response.
I have 7.11.0.584 matlab running on linux.
MATLAB needs to be told which directories to search over to access those m-files. Clearly it cannot be left to search over your entire disk drives. The MATLAB search path is a list of directories that will be searched in specific order to find your functions.
help addpath
help pathtool
You should never put those files anywhere in the official MATLAB toolbox directories. Choose an entirely separate directory.
Finally, be careful not to name your own functions to match the names of existing MATLAB functions. Otherwise, your very next question here will be why your code does not work properly. This is a common cause of strange and confusing bugs.
It seems you're having some trouble with addpath. Try opening the file in the matlab editor and adding a break point in the file. If the file is not on Matlab's path, matlab should ask if you want to change directory or add the file to the path, choose add to the path.
If this doesn't work, try changing the current working directory (displayed in the main window) to the same location as the m file and calling the function. If this doesn't work you're either getting the name wrong ar there's possibly something wrong with your installation.
Occasionally matlab has problems if it does not have write permission to the directory the file's in, so check that too, i.e. make sure admin rights aren't required for the directory or m file.
Oh, and try:
clear functions
to reload all functions into memory.
The function needs to be in MATLAB's path. Use pathtool to tell MATLAB where to find your function. Note that if you name a function the same name as an existing function, MATLAB will use whichever function it finds first according to the order that the paths are listed as you see them in pathtool.
Although coming late but I hope it will help someone.
If in the folder where the function you are calling is residing, there is any other function with the same name as one of the functions from MATLAB toolboxes, then Matlab will not recognize its license and therefore will disable the whole folder from execution, no matter it is properly added to the path. The help will display though.
In order to check it, type:
which name_of_func.m
and you will get the path with "%Has no license available" message.
If it is your own function, you should not get this message but only the path.
Therefore, find the function in this folder which has the same name as a MATLAB toolbox functions, and rename it. I will solve the problem :).
Best Regards
Wajahat

How can I list global variables in 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'.