MATLAB

What is Matlab?
MATLAB is a programming and numeric computing platform used by millions of engineers and scientists to analyze data, develop algorithms, and create models.
Tutorials
Software Carpentry provides a good resource for MATLAB:
Complete this tutorial, then come back here.
MathWorks also has an introductory MATLAB tutorial.
It is free to take and gives a good overview of what MATLAB can do.
Scripts
A plain text .m file containing Python code that is intended to be directly executed by the user is usually called a script
How to write a simple script:
Open a text file and save it as a filename with the suffix .m:
One good program to use for this is Sublime Text. Check it out.
Or better yet, Visual Studio Code.
Or even better yet, Positron (You will need to add the MATLAB Extension and have MATLAB R2023a or higher on your machine).
Or in the MATLAB console:
edit filename.mThis will create a text file called filename.m into the current directory. Then in the file, add your code:
disp('Hello World!')You can also store this file in a Script folder, which you can tell MATLAB to search. I generally put my MATLAB scripts in the directory MATLAB creates when it is installed (e.g., /Users/lj/Documents/MATLAB/Script on MacOS):
addpath('/Users/lj/Documents/MATLAB/Script')where /Users/lj/Documents/MATLAB/Script is replaced with the path to the folder you want MATLAB to look for your scripts. Note that the path is not saved when you quit MATLAB. If you would like to save the path, in the MATLAB console type:
savepath('/Users/lj/Documents/MATLAB/Script')This will save the path to your m-files for use next time. You only need to do this once.
Running your m-file
To run the m-file, in the MATLAB console type:
run filename.mor use the Run button in the MATLAB toolbar. If the m-file is in the current directory or in your saved Script folder, you should get
>>> run filename.m
Hello world!
>>printed to the console.
MRI with MATLAB
Since MATLAB R2024a, MathWorks has included a Medical Imaging Toolbox (in addition to SPM and its toolboxes) with some MRI and fMRI capabilities. Some functions in this toolbox may be useful if you use MATLAB a lot for your image processing. Ask IT to install it when they install MATLAB.
A useful review comes from (as always) Andy’s Brain Book:
Structural MRI
You can find a basic tutorial on how to load, view, register, and label a T1-weighted MRI using MathWorks’ Deep Learning algorithm at
- MRI Images And Data Structures
- Explore Slices from 3-D Image Volume with Anisotropic Voxel Spacing
- Brain MRI Segmentation Using Pretrained 3-D U-Net Network
- Segment and Analyze Brain MRI Scan Using AI.
- Brain MRI Segmentation Using Pretrained 3-D U-Net Network
Note that the Deep Learning algorithm has only been trained on healthy brains with no obvious structural abnormalities. Use caution when applying to clinical data.
Functional MRI
There are a variety of toolboxes and extensions for functional MRI, including
- SPM12 (see also SPM Overview from Andy’s Brain Book)
- Brain Connectivity Toolbox
- Compute Functional Connectivity from Brain fMRI
Call Python from MATLAB
You can access Python libraries directly from MATLAB by adding the py. prefix to the command name.
py.list({'This', 'is', 'a','list'}) % Call built-in function list
py.textwrap.wrap('This is a string') % Call the wrap function in the textwrap moduleCall R from MATLAB
You can call your R script from MATLAB by invoking the system command in the MATLAB console.
!Rscript /path/to/your/R_script.Rwhich does the equivalent of running your R script on the command line. You need to have R installed and your R script executable on the system path. In the terminal, type:
chmod +x R_script.Rand add it to your Script folder:
mv R_script.R /Users/lj/Documents/Script # I keep my scripts separate, but you should organize them by project
export PATH="$PATH:/Users/lj/Documents/Script"