MATLAB
In this section:
Description
According to the documentation of MATLAB, MATLAB is a numerical computing environment and programming language developed by MathWorks. It allows data analysis, algorithm development, model creation, and applications across diverse areas of engineering and science.
Available Versions
matlab/R2016b-9.1
matlab/R2019b (default)
Serial Job Submission
submit_matlab_serial.sh
#!/bin/bash
#SBATCH -J matlab_serial
#SBATCH -N 1
#SBATCH -n 1
#SBATCH -t 02:00:00
#SBATCH --mem=8G
export INPUT="script.m"
export OUTPUT="output.log"
module load matlab/R2019b
job-nanny matlab -batch "script"
Job Submission with .m File
submit_matlab_file.sh
#!/bin/bash
#SBATCH -J matlab_file
#SBATCH -N 1
#SBATCH -n 1
#SBATCH -t 04:00:00
#SBATCH --mem=16G
export INPUT="simulation.m data.mat"
export OUTPUT="results/"
module load matlab/R2019b
job-nanny matlab -nodisplay -nosplash -r "simulation; exit" > simulation.log
Parallel Job Submission (Parallel Computing Toolbox)
submit_matlab_parallel.sh
#!/bin/bash
#SBATCH -J matlab_parallel
#SBATCH -N 1
#SBATCH -c 8
#SBATCH -t 12:00:00
#SBATCH --mem=32G
export INPUT="parallel_script.m"
export OUTPUT="parallel_output/"
module load matlab/R2019b
# Create script with parallel configuration
cat > run_parallel.m << EOF
% Start worker pool
pool = parpool('local', $SLURM_CPUS_PER_TASK);
% Run main script
parallel_script;
% Close pool
delete(pool);
EOF
job-nanny matlab -nodisplay -nosplash -r "run_parallel; exit"
MATLAB Script Example
simulation.m
% Monte Carlo simulation
n_sim = 10000;
n_steps = 1000;
results = zeros(n_sim, 1);
tic;
for i = 1:n_sim
% Generate random trajectory
steps = randn(n_steps, 1);
trajectory = cumsum(steps);
% Calculate statistic of interest
results(i) = max(trajectory) - min(trajectory);
end
elapsed = toc;
% Statistics
mean_val = mean(results);
std_val = std(results);
histogram = histogram(results, 50);
% Save results
save('results.mat', 'results', 'mean_val', 'std_val', 'elapsed');
% Generate report
fid = fopen('report.txt', 'w');
fprintf(fid, 'Monte Carlo Simulation\n');
fprintf(fid, 'Number of simulations: %d\n', n_sim);
fprintf(fid, 'Execution time: %.2f seconds\n', elapsed);
fprintf(fid, 'Mean: %.4f\n', mean_val);
fprintf(fid, 'Standard deviation: %.4f\n', std_val);
fclose(fid);
% Figure
figure('Visible', 'off');
histogram(results, 50);
xlabel('Value');
ylabel('Frequency');
title('Distribution of results');
print('histogram', '-dpng');
Image Processing with Parallel Computing Toolbox
submit_matlab_image.sh
#!/bin/bash
#SBATCH -J matlab_image
#SBATCH -N 1
#SBATCH -c 8
#SBATCH -t 24:00:00
#SBATCH --mem=32G
export INPUT="images/*.png"
export OUTPUT="image_results/"
module load matlab/R2019b
cat > process_images.m << EOF
% List files
files = dir('images/*.png');
n_files = length(files);
% Start pool
pool = parpool('local', $SLURM_CPUS_PER_TASK);
% Process in parallel
parfor i = 1:n_files
name = files(i).name;
img = imread(['images/' name]);
% Processing
img_gray = rgb2gray(img);
img_filtered = medfilt2(img_gray);
edges = edge(img_filtered, 'canny');
% Save results
imwrite(img_filtered, ['image_results/filtered_' name]);
imwrite(edges, ['image_results/edges_' name]);
% Statistics
stats(i).name = name;
stats(i).mean = mean(img_filtered(:));
stats(i).std = std(double(img_filtered(:)));
end
% Save statistics
save('image_results/stats.mat', 'stats');
delete(pool);
EOF
job-nanny matlab -nodisplay -nosplash -r "process_images; exit"
Job Array for Multiple Parameters
submit_matlab_array.sh
#!/bin/bash
#SBATCH -J matlab_array
#SBATCH --array=1-10
#SBATCH -N 1
#SBATCH -n 1
#SBATCH -t 06:00:00
#SBATCH --mem=8G
PARAM=$SLURM_ARRAY_TASK_ID
export INPUT="param_${PARAM}.m"
export OUTPUT="run_${PARAM}/"
module load matlab/R2019b
mkdir -p run_${PARAM}
cd run_${PARAM}
cat > script_${PARAM}.m << EOF
% Specific parameter
param = $PARAM;
% Calculation
x = linspace(0, 10, 1000);
y = sin(param * x) .* exp(-x/10);
result = trapz(x, y.^2);
% Save
save(['result_${PARAM}.mat'], 'param', 'result');
% Report
fid = fopen('report.txt', 'w');
fprintf(fid, 'Parameter: %d\n', param);
fprintf(fid, 'Result: %.6f\n', result);
fclose(fid);
EOF
job-nanny matlab -nodisplay -nosplash -r "script_${PARAM}; exit"
Using Specific Toolboxes
submit_matlab_toolbox.sh
#!/bin/bash
#SBATCH -J matlab_toolbox
#SBATCH -N 1
#SBATCH -c 4
#SBATCH -t 12:00:00
#SBATCH --mem=16G
export INPUT="machine_learning.m"
export OUTPUT="ml_results/"
module load matlab/R2019b
cat > machine_learning.m << EOF
% Load data
data = load('training_data.mat');
X = data.X;
Y = data.Y;
% Split train/test
cv = cvpartition(size(X,1), 'HoldOut', 0.2);
X_train = X(cv.training,:);
Y_train = Y(cv.training);
X_test = X(cv.test,:);
Y_test = Y(cv.test);
% Train model (Statistics and Machine Learning Toolbox)
model = fitcecoc(X_train, Y_train);
% Evaluate
Y_pred = predict(model, X_test);
accuracy = sum(Y_pred == Y_test) / length(Y_test);
% Confusion matrix
cm = confusionchart(Y_test, Y_pred);
cm.Title = ['Accuracy: ' num2str(accuracy*100, '%.1f') '%%'];
exportgraphics(cm, 'confusion_matrix.png');
% Save model
save('trained_model.mat', 'model', 'accuracy');
EOF
job-nanny matlab -nodisplay -nosplash -r "machine_learning; exit"
Compiling MATLAB Applications
compile_matlab.sh
#!/bin/bash
#SBATCH -J compile_matlab
#SBATCH -N 1
#SBATCH -n 1
#SBATCH -t 01:00:00
#SBATCH --mem=4G
export INPUT="my_program.m"
export OUTPUT="my_program"
module load matlab/R2019b
job-nanny matlab -nodisplay -nosplash -r "mcc -m my_program.m; exit"
Running Compiled Application
run_compiled_matlab.sh
#!/bin/bash
#SBATCH -J run_compiled
#SBATCH -N 1
#SBATCH -n 1
#SBATCH -t 04:00:00
#SBATCH --mem=8G
export INPUT="data.dat"
export OUTPUT="results.dat"
# No need to load MATLAB module to run compiled app
job-nanny ./my_program data.dat
References
Documentation: https://www.mathworks.com/help/matlab/
Tutorials: https://www.mathworks.com/support/learn-with-matlab-tutorials.html
Parallel Computing Toolbox: https://www.mathworks.com/help/parallel-computing/
See also
Mathematica - Symbolic and numerical computing
R - Statistical computing
Running Simulations - How to submit jobs