Matlab Tips - labordynamicsinstitute/replicability-training GitHub Wiki
Tips and tricks for working with other people's Matlab programs
Adjusting paths globally in a Matlab program
Many authors don't do this, but when you replace hard-coded filepaths in Matlab, please do the following:
Say the author has code like
xlsread('C:\Users\me\submission\AEJMacro\yesterday\data.xlsx')
You should do the following:
- Copy
template-config.m
to the directory of the Matlab script you are running (see below how to run Matlab jobs) - Adjust the options.
- Add the line
config
to the main Matlab script.
Then
% Add this ONCE at the top of the program. Do NOT repeat it.
config
% wherever the hard-coded path appears, replace with
xlsread(fullfile(rootdir,'data.xlsx')
This works on any platform.
Saving a figure
See saveas
saveas(fig,filename)
For instance
bar(x);
saveas(gcf,'Barchart.png')
(for the report, always save as PNG, as it is easier to integrate into the report)
Making a Directory
If you an encounter an error that stems from the code attempting to write to a directory that does not exist, use the mkdir
function. For example, to create a new folder called "newFolder":
[status, msg] = mkdir('newFolder')
This will create the new folder. If the operation succeeds in the folder creation, it will show the following (if it fails you will get a 0):
status = logical
1
More info on this function can be found here.
I am running a Matlab program, but nothing has happened for an hour?
First, check that Matlab is currently busy. Look in the bottom left corner of the Matlab window for text that says "Busy". If it is there, wait! Matlab replications tend to take longer than Stata replications, and can run for 1 or 2 days without signs of progress. The wait time varies heavily for different programs, but if it runs for several days with no signs of progress, ask Lars or Flavio.
Undefined function or variable 'xyz'
?
I get an error Check the following for information on 'xyz'.
- readme file
- article
The .m program is using a software extension to MATLAB called "Dynare"
Do not attempt to install Dynare on CCSS or BioHPC computers.
However, it is not necessary to install Dynare, it is sufficient to unpack the ZIP files. Try the information below first:
- Be sure to include the
config.m
file as outlined above! - Inspect the last part of the
config.m
, it should have code much like the following:
%%% Dynare settings
%
% The following are possible Dynare settings. Uncomment the one you need.
% dynarepath = "/Applications/Dynare/4.6.1/matlab"
% dynarepath = "S:\LDILab\dynare\dynare-4.5.7\matlab"
%dynarepath = "L:\common\dynare-4.5.7\matlab"
%
% Then uncomment the following line:
%
%addpath(genpath(dynarepath))
- You will want the path for the system you are working on. If CCSS Cloud, use the
L:
path. If on BioHPC, you will need to add a path like/home/ecco_lv39/common/dynare
. - Then uncomment the
addpath()
statement at the end.
Your modified config.m
should look somewhat like this:
%%% Dynare settings
%
% The following are possible Dynare settings. Uncomment the one you need.
% dynarepath = "/Applications/Dynare/4.6.1/matlab"
% dynarepath = "S:\LDILab\dynare\dynare-4.5.7\matlab"
dynarepath = "L:\common\dynare-4.5.7\matlab"
%
% Then uncomment the following line:
%
addpath(genpath(dynarepath))
The Matlab program should now run, and call out to the Dynare software as needed.
If a specific version is mentioned and is not there, download it from the Dynare website - choose the ZIP version. Then unzip it to the L: drive
(move it around if necessary so you get the same structure as in the example above, i.e. L:\common\dynare\(SOME VERSION)\matlab
). Then include the addpath
command as before.
If you don't have access to the L drive yet, contact us.
Mention of MEX programs, what do I do?
(or: MEX files provided for Linux/MacOS, but you need to run on Windows)
- There should be
.cpp
files provided - if not, they should be requested from the author - From within Matlab, run
mex name_of_file.cpp
(though this should also be provided by the author).
Redirecting Matlab temporary directories
Matlab may write to temporary directories. If the filesystem is not large enough, it can be redirected.
#Create a job specific temp directory to avoid this
mkdir -p ~/matlabtmp/$PBS_JOBID
export MATLABWORKDIR=~/matlabtmp/$PBS_JOBID
Running MATLAB without the desktop GUI
Linux
matlab -nodisplay -r "addpath(genpath('.')); main" -logfile matlab.log
where main.m
is the Matlab program you want to run (you omit the .m
when calling it).
Windows
See these instructions for finding the Matlab binary on the system. However, this should work "out of the box" on CCSS-managed systems from the Bash prompt.
start matlab -nosplash -nodesktop -minimize -r "addpath(genpath('.'));main" -logfile matlab.log
where main.m
is the Matlab program you want to run (you omit the .m
when calling it).
Note that this will still open a Matlab window in the background (check your taskbar).
Getting the filename of the program running
[~, thisFileName, ~] = fileparts(mfilename('fullpath'));
Getting the Matlab versions and toolboxes
ver
Unzipping files from within Matlab
display 'Unzipping a few large folders'
cd benchmark; unzip('benchmark.zip'); cd(rootdir);
cd sensitivity; unzip('sensitivity.zip'); cd(rootdir);
cd richer; unzip('richer.zip'); cd(rootdir);