Azure Function is a serverless compute service that enables user to run event-triggered code without having to provision or manage infrastructure. Being as a trigger-based service, it runs a script or piece of code in response to a variety of events.
Azure Functions can be used to achieve decoupling, high throughput, reusability and shared. Being more reliable, it can also be used for the production environments.
However, as of March 2021, Azure Function is only supporting C#, Javascript, F#, Java, Powershell, Python, and TypeScript.They are mainly opensource language.
In this documentation, it will walk you through two ways to embed MATLAB Algorithm into Azure Function.
- through MATLAB Coder > C# (.dll)
- through MATLAB Compiler SDK > Python Package > Docker Image
Generally, solution(1) would allow your Azure function response faster than solution(2), as the C# is directly supported by Azure function natively. For solution (2), it requires more storage space for the docker image. Intuitively, solution (2) might be preferable due to MATLAB Runtime supports almost all MATLAB Function but MATLAB Coder is only supporting limited function for code generation.
Function supported for code generation (MATLAB Coder): MathWorks Documentation
Any inquiries, you may email me : kevin.chng@techsource-asia.com, I will try to help if my time is allowed.
In this solution, we will generate standalone c/c++ source code from MATLAB algorithm and wrap it as dll. By default, Azure function supports 32bits Platform, it might require more troubleshooting if we try to switch it to 64bits. Therefore, the generated MATLAB .dll has to be 32bits. This process could be done using MATLAB Coder and Visual Studio Compiler. Subsequently, we embed this .dll into our Azure Function dotnet framework and later deploy to Azure Cloud.
Before you begin, you must have the following:
- Azure Function Tool
- Microsoft Visual Studio 9.0/10.0/11.0/12.0/14.0/15.0
- MATLAB & MATLAB Coder
The links below are my references for this solution:
- Generate 32bits dll using MATLAB Coder
- Create C# function in Azure for Command Line (Azure Function)
Below is the detail steps to deploy Azure Function through MATLAB Coder:
In this solution, we will deploy code to Azure Function as a custom docker container using a linux base image. This customer image allows us to install additional dependency or configuration that isn't provided by the built in image. We insall MATLAB runtime (MCR) inside this custom docker container to provide the environment to run MATLAB code.
Although you are allowed to install additional dependency inside the custom docker, it is still restricted us to only use the following languages: C#,Java,Javascript,PowerShell,Python,TypeScript
For this solution, I use python as a bridge to linkup Azure Function and MCR. However, when you are selecting other languages, kindly make sure that your selected linux base image is 64bit which MCR requires 64bits environment.
Deploying function code in a custom Linux container requires Premium plan or a Dedicated (App Service) plan hosting. Therefore, completing this example might incurs costs of a few US dollars in your Azure account, which you can minimize by cleaning-up resources when you're done.
Before you begin, you must have the following:
- Azure Function Tool
- The Azure CLI version 2.4 or later.
- Python 3.8 (64-bit), Python 3.7 (64-bit), Python 3.6 (64-bit), which are supported by Azure Functions.
- Docker and Docker ID
- MATLAB and MATLAB Compiler SDK
The links below are my references for this solution:
- Generate Python Package and Build Python Application
- Create a function on Linux using a custom container (Azure Function)
Use Window as development environment : Example
Use linux as development environment : Example
With Azure Function Docker Conatiner, it allows us to install MATLAB Runtime into it. Besides python in solution 2, you may consider to use other supported language for your Azure Docker Container.
You may refer to their supported list of docker image and ensure your selected image is 64bits: https://hub.docker.com/_/microsoft-azure-functions-base


