GithubHelp home page GithubHelp logo

nascimentolwtn / matlabintegretmulapp Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zijingmao/matlabintegretmulapp

0.0 2.0 0.0 4.41 MB

This is an example of using matlab function from android applications through android studio, following the instruction, one can integrate supported matlab function to speed up your Android app.

Java 0.54% Makefile 5.62% C 34.92% CSS 0.02% HTML 58.05% JavaScript 0.78% Batchfile 0.05% C++ 0.02% M 0.01% Objective-C 0.01%

matlabintegretmulapp's Introduction

Integrate Matlab Function Into Android App

###Required software and tools

Note: NDK required to be installed first in Android studio Matlab coder required to be installed first in Matlab

###Step 1. Matlab to C/C++ code

Write simple multiplication matlab code (see .\matlab_source_code)

% test function
function [ c ] = matrix_multiplication( a, b )
    c = a * b;
end

% test script
a = [1, 2, 3; 4, 5, 6];
b = [1, 2; 3, 4; 5, 6];
c = matrix_multiplication(a, b);

Convert Matlab code to C code using coder (see ...app\src\main\jni\src\MatrixMultiplication) Remember to generate C code at the last step.

See Ref. [1-4]

###Step 2. C/C++ code with JNI wrapper

  1. Create jni folder under main folder
  2. Put C/C++ code into jni folder
  3. Write Android.mk & Application.mk file following given references
  4. Write interface file (in this example is: libmatrixmultiplication.i) following given reference
  5. Execute command line to compile C/C++ to .so using NDK (this will auto generate .so file into ...app\src\main\libs\armeabi)
>$cd app\src\main
>#exec swig with interface to generate swig wrapper for C/C++
>$c:\swigwin-3.0.8\swig.exe -java -package com.matrixmultiplication -outdir ..\java\com\matrixmultiplication -o libmatrixmultiplication_wrap.c libmatrixmultiplication.i
>#link all source and header based on the configuration of Android.mk file
>#notice the include $(BUILD_SHARED_LIBRARY) should be at last to prevent errors, if error still exist, see _ref[9]_ and _Potential Problems for NDK_
>$c:\NVPACK\android-ndk-r10e\ndk-build

See Ref. [5-10]

###Step 3. Call .so lib from Android Studio

  1. Configure your build.gradle & gradle.properties following ref[6,9] will most likely avoid building errors, if there still exists please google, should be easy/common problems
  2. Call the compiled library using code under your wanted android activity or service:
    static {
        System.loadLibrary("MatrixMultiplication");
    }
  1. Use the line to call function inside .so lib in onCreate or wherever you desired
    // DON'T FORGET TO IMPORT YOUR PACKAGE
    MatrixMultiplication.matrix_multiplication(a, b, c);

Note: If you want to use more straightforward JNA library, please refer to ref[11-12]

####Reference

  1. Matlab coder with iPhone & Android video tutorial
  2. Matlab coder with Android code example
  3. Matlab coder video tutorial (Chinese)
  4. Matlab coder document
  5. SWIG and Android
  6. Android Studio, gradle and NDK integration
  7. Compiling and Using a C++ Library on Android with Android Studio
  8. Wrapping C++ code with JNI
  9. Android NDK in Android Studio with SWIG
  10. Wrapping C arrays with Java arrays
  11. Java Native Access(JNA)
  12. Easier way to access native code

####Potential Problems for NDK

Notice: Normally the problem of not able to generate .so file is because Application.mk.

If the problem is like:

error: vector: No such file or directory error: iostream: No such file or directory error: stdexcept: No such file or directory ...

The question might also be in the swig interface file (see ...\app\src\main\jni\libmatrixmultiplication.i) remove following code

%include "std_vector.i"
%include "std_string.i"
%include "exception.i"
...

if these headfiles are not required in your C/C++ code.

matlabintegretmulapp's People

Contributors

zijingmao avatar

Watchers

James Cloos avatar Luiz Wagner Tavares Nascimento avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.