简体   繁体   中英

Using a MATLAB Function in Xamarin Android C#

I've been trying to make a Xamarin Android application which uses the functions of MATLAB. To test that it works or not I made a dummy MATLAB program to square the given input. I created DLL of this MATLAB function and added it as Reference in my Xamarin Android application in VS(ie DLL="squarefunction2" & Class="squarefunction").

The problem I'm facing is an exception when the MATLAB function is called through c# on a Button click event. The Exception is System.Typ​eInitializ​ationExcep​tion .

I have already checked for 32/64 bit mismatch. Both my VS and MATLAB are x64. My configuration manager is also on x64 platform. And I have also tried to change .NET versions 4.6.1 ,4.5 ,4 etc.

Following are the codes-

C#

using Android.App;
using Android.Widget;
using Android.OS;
using System;
using squarefunction2;
using MathWorks.MATLAB.NET.Arrays;

namespace HRMUMM4.5NET
{
[Activity(Label = "HRMUMM4.5NET", MainLauncher = true)]
public class MainActivity : Activity
{
    Button SQbutton;
    EditText inputtext, resultbox;
    squareclass obj = null;
    MWNumericArray input = null;
    MWNumericArray output = null;
    MWArray[] result = null;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.Main);
        FindLayouts();
        SQbutton.Click += SQbutton_Click;
    }

    private void SQbutton_Click(object sender, EventArgs e)
    {
        try
        {
            // Instantiate your component class.
            obj = new squareclass();
            // Invoke your component.
            //input = Convert.ToUInt64(inputtext.Text);
            input = 5;
            result = obj.squarefunction(1, input);
            // Extract the Square you created from the first index of result
            output = (MWNumericArray)result[0];
            // print the output.
            Console.WriteLine(output);
        }
        catch
        {
            throw;
        }
    }

    void FindLayouts()
    {
        SQbutton = FindViewById<Button>(Resource.Id.SQbutton);
        inputtext = FindViewById<EditText>(Resource.Id.inputtext);
        resultbox = FindViewById<EditText>(Resource.Id.resultbox);
    }
}
}

MATLAB-

function sqres = squarefunction(num)
sqres=num*num;

Exception Occurring on Button click-

Unhandled Exception:

System.TypeInitializationException: The type initializer for 'squarefunction2.squareclass' threw an exception.

Any help would be really appreciated, thanks.

Just like compiled .NET and Java codes are running by being tied to some runtime that must be installed on the target machine (ie respectively .NET runitme and Java runtime), compiled Matlab code, even if encapsulated in C# assembly, runs by being tied to some runtime that must installed on target machine too.

The Matalb runtime itself is in fact a huge set of native libraries (about 1.5 GB once installed) and is intented to be deployed on desktop computers only, not mobile phones.

Here are a few alternatives to "integrate" matlab code in your mobile phone application:

  • If your matlab code is very simple you may either transpose it by hand to other language or use Matlab coder to deploy it to IOs or Android phones (see example video here )

  • If your matlab code is far more complex, you may deploy it as a web service so you can access it from your mobile

  • Very alternatively you may turn to python for android ... but i don't know much about that, see kivy for instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM