To generate code for 'simplefn' using OpenBLAS routines, first define a MATLAB class openblascallback.m as shown below. This example is adapted from the documentation. Note that the library shared object and header locations, as well as the header name, depend on the Operating System and the specific BLAS library or package installed. For detailed instructions, you can access the release-specific documentation by executing the following command in the MATLAB R2020b command window:
>> web(fullfile(docroot, 'coder/ref/coder.blascallback-class.html'))
classdef openblascallback < coder.BLASCallback
methods (Static)
function updateBuildInfo(buildInfo, buildctx)
libPriority = '';
libPreCompiled = true;
libLinkOnly = true;
libName = 'libopenblas.so';
libPath = fullfile('/usr/lib/x86_64-linux-gnu');
incPath = fullfile('/usr/include/x86_64-linux-gnu');
buildInfo.addLinkFlags('-lpthread');
buildInfo.addLinkObjects(libName, libPath, ...
libPriority, libPreCompiled, libLinkOnly);
buildInfo.addIncludePaths(incPath);
end
function headerName = getHeaderFilename()
headerName = 'cblas.h';
end
function intTypeName = getBLASIntTypeName()
intTypeName = 'blasint';
end
end
end
Then create the following Code Generation script simplefn_script.m:
cfg = coder.config('lib', 'ecoder', false);
cfg.GenerateReport = true;
cfg.Toolchain = 'GNU gcc/g++ | gmake (64-bit Linux)';
cfg.CustomBLASCallback = 'openblascallback';
codegen -config cfg simplefn -args {zeros(1000),zeros(1000)} -report
Running simplefn_script.m should successfully generate code with OpenBLAS routines. The simplefn.c now contains:
void simplefn(const double inputArg1[1000000], const double inputArg2[1000000],
double outputArg1[1000000])
{
cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, (blasint)1000, (blasint)
1000, (blasint)1000, 1.0, &inputArg1[0], (blasint)1000,
&inputArg2[0], (blasint)1000, 0.0, &outputArg1[0], (blasint)1000);
}
Please follow the link below to search for the required information regarding the current release:
Starting from MATLAB R2025b, MathWorks provides official callback classes for popular BLAS and LAPACK libraries. These ready-to-use callbacks simplify integration with libraries such as OpenBLAS and MKL, making it easier to enhance performance in standalone code generation.