How to Integrate Matlab Code Library with Android

How to integrate Matlab code library with Android?

If you have an additional product, MATLAB Builder JA for Java, you can produce a .jar file from your MATLAB code.

However, the .jar file requires the MATLAB Compiler Runtime (a freely redistributable component that you get with MATLAB Compiler and MATLAB Builder products) to be present. The MCR has a much larger footprint than is suitable for the typical Android device (it's like a copy of MATLAB itself, without the user interface).

You could think about either

  1. Running your MATLAB .jar file remotely on a server, and having your Android application connect to it, or
  2. Instead of using MATLAB Compiler and Builder products, use MATLAB Coder, which will convert a subset of the MATLAB language directly into C code. This C code doesn't require the MCR, and could be compiled to run directly on Android. Make sure your MATLAB algorithm falls within, or can be expressed in, the appropriate subset of the MATLAB language.

Edit: As of R2015a, functionality from MATLAB Builder JA for Java has been replaced by a new product, MATLAB Compiler SDK.

Can we implement MATLAB code on Android?

If you have an additional product, MATLAB Builder JA for Java, you can produce a .jar file from your MATLAB code.

However, the .jar file requires the MATLAB Compiler Runtime (a freely
redistributable component that you get with MATLAB Compiler and MATLAB
Builder products) to be present. The MCR has a much larger footprint
than is suitable for the typical Android device (it's like a copy of
MATLAB itself, without the user interface).

You could think about either

Running your MATLAB .jar file remotely on a server, and having your Android application connect to it, or

Instead of using MATLAB Compiler and Builder products, use MATLAB
Coder, which will convert a subset of the MATLAB language directly
into C code. This C code doesn't require the MCR, and could be
compiled to run directly on Android. Make sure your MATLAB algorithm
falls within, or can be expressed in, the appropriate subset of the
MATLAB language.

More infos can be found here from Sam Roberts post

include Matlab Coder dll in Android Studio

See the following documentation link:

http://www.mathworks.com/help/images/understanding-code-generation-with-image-processing-toolbox.html

Also see the beginning of the following page:

http://www.mathworks.com/help/images/list-of-supported-functions-with-usage-notes.html

You should try a hardware target different than "MATLAB Host Computer". When targeting "MATLAB Host Computer" if there is a platform specific shared library implementation available for the image processing function it is used. But for other targets coder will use a standalone c-code implementation for the function if it is available.

creating a game in matlab and port it to android.

You can try to do that. I also am much more confortable programming in Matlab. However, I suggest that you try other language.

I started to learn Python 2.7 a few months ago. You will quickly feel confortable in this language and it offers more advantages if you want to create a cool game.

Python has some modules (numpy and scipy) that have a lot of the functionalities that you are used to in Matlab.

There are a lot of online courses to learn Python also.

You can also try to learn Android or Java, but I think that's a higher jump to take.
Good luck!

Flutter - run MATLAB function on mobile app (primary Android)

You can compile MATLAB/Simulink code into C code, which you can then integrate into your flutter app using dart:ffi. While this may be tricky to set up first (be sure to follow the instructions from the flutter documentation, it's pretty good), it certainly works and I've done it before (only the flutter integration, not the Matlab to C part). Once it's set up, it's relatively easy to maintain. As you may have to write C/C++ wrappers for the generated C code some C knowledge may be helpful, but I don't think it's not worth trying.

Using existing C code files to build in Android and pass information from my java code to the C code

Your strategy would be the following:

  1. Create Android project with native C/C++ code support, e.g. like here: https://developer.android.com/studio/projects/add-native-code. And your goal is to create the following chain:

    [Your Android Java app] --> [JNI Java API] --> [JNI native] --> [MatLab native]

    JNI is just to marshal a Java call to a targeted MatLab function.

  2. Generate a C code only for a Matlab function that you really need. Check documentation (https://www.mathworks.com/help/dsp/ug/generate-c-code-from-matlab-code-1.html):

    You do not have to generate code for the entire MATLAB application. If you have specific portions that are computationally intensive, generate code from these portions in order to speed up your algorithm.
    Make Code Suitable for Code Generation

  3. Generate JNI with methods designed by yourself: e.g. you will have MatLabJniApi.java (JNI Java API) and you will get com_your_MatLabJniApi.c (and com_your_MatLabJniApi.h) as a result of javac -h . MatLabJniApi.java command (JNI native implementation).

  4. Finally, call MatLab function from generated JNI native C/C++ files:

    #include <matlab_header_with_foobar.h>
    ...
    JNIEXPORT void JNICALL Java_com_your_MatLabJniApi_foobar (JNIEnv* env, jobject thisObject) {
    ...
    foobar_from_matlab();
    ...
    }

    Keep JNI clear and minimal: it is just a bridge to the code produced by MatLab.

  5. Put all C/C++ files into a single folder (cpp/ or jni/, depending on your Android project structure), so they will compile together by cmake (or by ndk-build, depending on a chosen method to compile a JNI).

Good luck!

P.S.: example of Android.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := mymatlab # Name of your module
LOCAL_SRC_FILES := 1.c 2.c 3.c # Sources, and here you can try $(wildcard *.c)
include $(BUILD_STATIC_LIBRARY) # This means to build libmymatlab.a

How can I use matlab code in C# to work in windows phone?

You have two options to integrate MATLAB code into a Windows phone app.

  1. Deploy your code to a web service on a remote server with MATLAB Builder NE, and have your C# app call that.
  2. Use MATLAB Coder to convert your algorithm to C, and integrate that C code directly into your C# app. You would need to make sure that your algorithm falls into the subset of the MATLAB language supported by MATLAB Coder, but if it is primarily a signal processing algorithm it is likely that it will.

I'm not familiar with a specific example that I can point you to of a Windows phone app, but I know that MathWorks have examples of how to apply the second option to generate C code that is integrated into an Objective C application on iPhone.

Ignore the possibility of deploying your code with MATLAB Compiler and integrating the result into your C# app. The executables/libraries created by MATLAB Compiler depend on MATLAB Compiler Runtime (MCR), which has a much larger footprint than is suitable for a mobile app.



Related Topics



Leave a reply



Submit