Going back to avoid future mistakes

As VersionBay analyzes more MATLAB code it builds up a knowledge base of common programming practices that could be improved. The knowledge base is the starting point for the recommendations delivered with the Advance Service. Here are a few examples of Best Practices, Refactoring MATLAB Code and Improving MATLAB Programs.


Best Practices

There are thousands of small things which can make your MATLAB code better. Many of them are caught by the MATLAB Code Analyzer, for others, there are third party solutions to help find them. VersionBay maintains a list of best practices which make the code easier to read and maintain. Notice how this example allows the same code to work on Windows and macOS/Linux-based systems.

data = readtable('data\file.xlsx');

becomes

data = readtable(fullfile('data', 'file.xlsx'));

if a < 0
   t = 0:10;
   b = t.*t;
   plot(t, b)
else
   t = 0:10;
   b = t.^3; 
   plot(t, b)
end

becomes

t = 0:10;

if a < 0
   b = t.*t;
else
   b = t.^3; 
end

plot(t, b)

Refactoring MATLAB Code

There are many types of code refactoring that VersionBay looks for. These are common best practices used in most programming languages. However, the MATLAB Editor has limited capabilities to support refactoring. As a result MATLAB code often could do with a clearer view.

The idea of code refactoring is to make it easier to maintain, and read. The goal is not to optimize for performance.


Improving MATLAB Programs

There are many new features in MATLAB where sometimes a new function or datatype can help make the code clearer and easier to maintain. The import data example highlights this by replacing the automatically generated script from the Import Wizard with a single line of code.

Another good example is the splitapply function used in the code below:

load patients
byGroup = findgroups(Gender);
splitapply(@mean, Height, byGroup)

Which is a very clear and easy way to find all the categories in arrays or tables, store the indices, and calculate the mean per category. Most MATLAB programmers would do something like:

load patients
Gender = categorical(Gender);
GenderList = unique(Gender);
for idx = 1:length(GenderList)
    idxHeight = Height(Gender==GenderList(idx));
    HeightAvg(idx) = mean(idxHeight);
end
%% Import data from spreadsheet
% Script for importing data from the following spreadsheet:
%
%    Workbook: patients.xlsx
%    Worksheet: Sheet1
%
% Auto-generated by MATLAB on 22-Jan-2019 09:51:43

%% Setup the Import Options
opts = spreadsheetImportOptions("NumVariables", 5);

% Specify sheet and range
opts.Sheet = "Sheet1";
opts.DataRange = "A2:E101";

% Specify column names and types
opts.VariableNames = ["LastName", "Age", "Gender", "Smoker", "Weight"];
opts.VariableTypes = ["string", "double", "categorical", "string", "double"];
opts = setvaropts(opts, [1, 4], "WhitespaceRule", "preserve");
opts = setvaropts(opts, [1, 3, 4], "EmptyFieldRule", "auto");

% Import the data
patients = readtable("patients.xlsx", opts, "UseExcel", false);

%% Clear temporary variables
clear opts

becomes

patients = readtable('patients.xlsx');

Get Started Now by Contacting Us