IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Java Discussion :

Lancer une fonction de MatLab par Java


Sujet :

Langage Java

  1. #1
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2015
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 29
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juillet 2015
    Messages : 5
    Points : 3
    Points
    3
    Par défaut Lancer une fonction de MatLab par Java
    Bonjour à tous,

    Je code actuellement un programme sur Java de traitement de données. Je passe par une fonction codée sur MatLab pour traiter une partie des données.
    J'utilise donc matlabcontrol-4.1.0. comme API, et j'ai quelques soucis.
    En effet, la fonction a plusieurs arguments, et je veux créer une fonction Java me permettant de rentrer les arguments que je désire, et ensuite que ces arguments soient intégrés à la fonction et celle-ci lancée dans MatLab.
    Le problème est que ces arguments sont pour certains des chaînes de caractères, et que pour lancer ma fonction sur MatLab, je dis à Java de rentrer une ligne de commande directement dans MatLab, ligne contenant le nom de la fonction et les arguments.
    Voici le script :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
     
     
    import matlabcontrol.MatlabConnectionException;
    import matlabcontrol.MatlabInvocationException;
    import matlabcontrol.MatlabProxy;
    import matlabcontrol.MatlabProxyFactory;
     
     
    	public class MtCoonect
    	{
    	    public MtCoonect()
    	    {
    	        super();
    	    }
     
     
    	    /*
    	     * Definition of the Eulerian function :
    	     * It is used to convert all the Data files sourced by the Inertial Units into files read by ArtiSynth and the Input probes.
    	     * In order to do that, we use MatLab and througth the Eulerian function, we give
    	     * 
    	     * 
    	     */
    	    public static void Eulerian(String[] BASE, NAME, FILE, Int Number) 
                throws MatlabConnectionException, MatlabInvocationException
    	    { 
                            DATA = "'" + "BASE" + "'";
                            BASE = "'" + "BASE" + "'";
                            FILE = "'" + "FILE" + "'";
     
    	    	        //Create a proxy, which we will use to control MATLAB
     
    	        MatlabProxyFactory factory = new MatlabProxyFactory();
    	        MatlabProxy proxy = factory.getProxy();
    	        proxy.isExistingSession ();
     
     
     
    	       //	proxy.feval ("Eulerian", Object[DATA,Number, NAME, FILE ] );
     
    	        //Create the directory and the data files.
     
    	        //proxy.invocationSucceeded();
     
     
    	        //Disconnect the proxy from MATLAB and close MatLab
    	         proxy.exit ();
    	        proxy.disconnect();
     
     
    	    }
    	    }
    Mais comme vous pouvez vous en douter, ça ne marche pas trop.

    Un exemple de la commande à retrouver sur MatLab est :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    Eulerian('DATA', 8,'Marc','Test')
    J'aimerais donc de l'aide pour savoir comment pouvoir appeler ma fonction dans MatLab en pouvant faire varier les arguments de la fonction, bien que ce soient des chaînes de caractères et des nombres.

    En espérant que vous pourrez m'aider, et merci d'avoir lu mon post,
    GaTaLek

  2. #2
    Membre confirmé Avatar de ruscov
    Homme Profil pro
    Architecte de système d'information
    Inscrit en
    Mars 2007
    Messages
    347
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : Belgique

    Informations professionnelles :
    Activité : Architecte de système d'information

    Informations forums :
    Inscription : Mars 2007
    Messages : 347
    Points : 500
    Points
    500
    Par défaut
    Bonjour,

    Je ne comprend pas bien ton soucis.
    Montre nous ton erreur!

    Un truc que je vois de faux dependant :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    proxy.feval ("Eulerian", Object[DATA,Number, NAME, FILE ] );
    Si on regarde l'api, il est indiqué qu'il recoit un nombre variable d'argument de type Object et pas un Object[] : java.lang.Object... args
    Cela devrait plutôt donner quelque chose comme ca :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    proxy.feval ("Eulerian", DATA,Number, NAME, FILE );
    Mes logiciels n’ont jamais de bug. Ils développent juste certaines fonctions aléatoires.

  3. #3
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2015
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 29
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juillet 2015
    Messages : 5
    Points : 3
    Points
    3
    Par défaut
    Bonjour,

    Désolé du retard.

    Voici le code d'erreur retourné, avec la correction réalisée :


    Exception in thread "main" matlabcontrol.MatlabInvocationException: Method did not return properly because of an internal MATLAB exception
    at matlabcontrol.MatlabInvocationException$Reason.asException(MatlabInvocationException.java:62)
    at matlabcontrol.JMIWrapper$MatlabThreadOperations.returningFeval(JMIWrapper.java:459)
    at matlabcontrol.JMIWrapper$MatlabThreadOperations.feval(JMIWrapper.java:406)
    at matlabcontrol.JMIWrapper$6.call(JMIWrapper.java:180)
    at matlabcontrol.JMIWrapper$6.call(JMIWrapper.java:176)
    at matlabcontrol.JMIWrapper$9.run(JMIWrapper.java:307)
    at com.mathworks.jmi.NativeMatlab.dispatchMTRequests(NativeMatlab.java:475)
    at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
    at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
    at sun.rmi.server.UnicastRef.invoke(Unknown Source)
    at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown Source)
    at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)
    at matlabcontrol.$Proxy1.feval(Unknown Source)
    at matlabcontrol.RemoteMatlabProxy$6.invoke(RemoteMatlabProxy.java:285)
    at matlabcontrol.RemoteMatlabProxy$6.invoke(RemoteMatlabProxy.java:281)
    at matlabcontrol.RemoteMatlabProxy.invoke(RemoteMatlabProxy.java:182)
    at matlabcontrol.RemoteMatlabProxy.feval(RemoteMatlabProxy.java:280)
    at artisynth.models.AQM_Project.MtCoonect.Eulerian(MtCoonect.java:66)
    at artisynth.models.AQM_Project.MtCoonect.main(MtCoonect.java:90)
    Caused by: com.mathworks.jmi.MatlabException: Error: <a href="matlab: opentoline('C:\Users\Kiwi\Desktop\TraitementQuaternions\manip_09_07_2015\Eulerian.m',40,190)">File: Eulerian.m Line: 40 Column: 190
    </a>Unexpected MATLAB expression.
    at com.mathworks.jmi.NativeMatlab.SendMatlabMessage(Native Method)
    at com.mathworks.jmi.NativeMatlab.sendMatlabMessage(NativeMatlab.java:266)
    at com.mathworks.jmi.MatlabLooper.sendMatlabMessage(MatlabLooper.java:120)
    at com.mathworks.jmi.Matlab.mtFevalConsoleOutput(Matlab.java:1690)
    at matlabcontrol.JMIWrapper$MatlabThreadOperations.returningFeval(JMIWrapper.java:420)
    at matlabcontrol.JMIWrapper$MatlabThreadOperations.feval(JMIWrapper.java:406)
    at matlabcontrol.JMIWrapper$6.call(JMIWrapper.java:180)
    at matlabcontrol.JMIWrapper$6.call(JMIWrapper.java:176)
    at matlabcontrol.JMIWrapper$9.run(JMIWrapper.java:307)
    at com.mathworks.jmi.NativeMatlab.dispatchMTRequests(NativeMatlab.java:475)
    Je ne vois pas trop le problème. Peut-être est-ce au niveau du workspace de MatLab. Je n'ai pas réussi à bien faire marcher le changement de répertoire à l'aide d'un

    proxy.feval('cd('....')');

    Le problème est qu'il n'aime pas le ":" dans l'adresse.

    Je redonne le code en passant :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
     
     
    import matlabcontrol.MatlabConnectionException;
    import matlabcontrol.MatlabInvocationException;
    import matlabcontrol.MatlabProxy;
    import matlabcontrol.MatlabProxyFactory;
    import matlabcontrol.extensions.MatlabNumericArray;
    import matlabcontrol.extensions.MatlabTypeConverter;
     
    	public class MtCoonect
    	{
    	    public MtCoonect()
    	    {
    	        super();
    	    }
    	    //////////////// Connecting to MatLab and basic calculation //////////////////
    //connexion au port:
     /*
    	    public static void main(String[] args) throws MatlabConnectionException, MatlabInvocationException
    	    {
    	        //Create a proxy, which we will use to control MATLAB
    	        MatlabProxyFactory factory = new MatlabProxyFactory();
    	        MatlabProxy proxy = factory.getProxy();
     
    	        //Display 'hello world' just like when using the demo
    	        proxy.eval("run('hello world')");
     
     
    	        //Set a variable, add to it, retrieve it, and print the result
    	        proxy.setVariable("a", 5);
    	        proxy.eval("a = a + 6");
    	    //    Object result = proxy.getVariable("a"); //  récupération code du résultat
    	        double result = ((double[]) proxy.getVariable("a"))[0]; // récupération du double
    	        System.out.println("Result: " + result);
     
     
    	        //Disconnect the proxy from MATLAB
    	        proxy.disconnect();*/
    	//////////////////////////////// Numeric Arrays //////////////////////    
     
     
     
    	                /*
    	                 * Definition of the Eulerian function :
    	                 * It is used to convert all the Data files sourced by the Inertial Units into files read by ArtiSynth and the Input probes.
    	                 * In order to do that, we use MatLab and througth the Eulerian function, we give
    	                 * 
    	                 * 
    	                 */
    	    public static void Eulerian(String BASE,String NAME, String FILE, int Number) 
    	                throws MatlabConnectionException, MatlabInvocationException
    	                { 
    	                            BASE = "'" + "BASE" + "'";
    	                            NAME = "'" + "NAME" + "'";
    	                            FILE = "'" + "FILE" + "'";
     
    	                            //Create a proxy, which we will use to control MATLAB
     
    	                    MatlabProxyFactory factory = new MatlabProxyFactory();
    	                    MatlabProxy proxy = factory.getProxy();
    	                    proxy.isExistingSession ();
     
     
     
    	                    proxy.feval ("Eulerian", BASE,Number, NAME, FILE );
     
    	                    //Create the directory and the data files.
     
    	                    //proxy.invocationSucceeded();
     
     
    	                    //Disconnect the proxy from MATLAB and close MatLab
    	                    // proxy.exit ();
    	                 //   proxy.disconnect();
     
     
    	                }
     
     
     
     
     
    	    public static void main(String[] args) throws MatlabConnectionException, MatlabInvocationException
    	    {  int Number =9;
    	       String Base = "DATA";
    	       String Name = "Ang";
    	       String File = "Test";
     
    	          Eulerian(Base, File,Name,Number);
     
     
     
     
    	        //Disconnect the proxy from MATLAB and close MatLab
     
     
    	    }
    	    }
    Merci beaucoup pour l'aide déjà apportée.
    GaTaLek

  4. #4
    Membre confirmé Avatar de ruscov
    Homme Profil pro
    Architecte de système d'information
    Inscrit en
    Mars 2007
    Messages
    347
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : Belgique

    Informations professionnelles :
    Activité : Architecte de système d'information

    Informations forums :
    Inscription : Mars 2007
    Messages : 347
    Points : 500
    Points
    500
    Par défaut
    De ce que je comprends de l'erreur c'est que ton fichier matlab contient des erreurs... Rien à voir avec Java.
    Mes logiciels n’ont jamais de bug. Ils développent juste certaines fonctions aléatoires.

  5. #5
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2015
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 29
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juillet 2015
    Messages : 5
    Points : 3
    Points
    3
    Par défaut
    Bonjour,

    Un problème pourrait être que une des fonctions qu'utilise mon programme MatLab n'est pas disponible sur ce Matlab ( la fonction quat2angle), mais l'erreur n'est pas celle-ci.

    Je vais regarder encore, mais normalement le programme MatLab marche bien sur un Matlab avec la fonction.

    Merci,
    GaTaLek.

  6. #6
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2015
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 29
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juillet 2015
    Messages : 5
    Points : 3
    Points
    3
    Par défaut
    Le problème est réglé : j'ignorais que MatLab comprenait ce qu'était un String en Java, et donc j'avais des guillemets qui traînaient.

    Voici le code final, si quelqu'un veut s'en inspirer pour son compte.


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    import matlabcontrol.MatlabConnectionException;
    import matlabcontrol.MatlabInvocationException;
    import matlabcontrol.MatlabProxy;
    import matlabcontrol.MatlabProxyFactory;
     
     
    	public class MtCoonect
    	{
    	    public MtCoonect()
    	    {
    	        super();
    	    }
    	    //////////////// Connecting to MatLab  //////////////////
     
    	    public static void Eulerian(String BASE,String NAME, String FILE,int Numberd, int Numberf, double pas) 
    	                throws MatlabConnectionException, MatlabInvocationException
    	                {	                            
    	                            //Create a proxy, which we will use to control MATLAB
    	                    MatlabProxyFactory factory = new MatlabProxyFactory();
     
    	                    MatlabProxy proxy = factory.getProxy();
    	                    proxy.isExistingSession ();
     
     
     
    	                    proxy.feval ("Eulerian", BASE,Numberd, Numberf, NAME, FILE, pas );
     
     
     
     
    	                    //Disconnect the proxy from MATLAB and close MatLab
    	                     proxy.exit ();
                                 proxy.disconnect();
    	                }
     
     
     
    	    public static void main(String[] args) throws MatlabConnectionException, MatlabInvocationException
    	    {  int Number =12;
    	       int Numver = 1;
    	       String Base = "DATA";
    	       String Name = "SPECIMEN";
    	       String File = "Test";
    	       double pas =0.01;
     
    	          Eulerian(Base, File,Name,Numver, Number, pas);
     
     
    	    }
    	    }
    Merci pour ton aide ruscov.

    GaTaLek

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. lancer une fonction MATLAB depuis un code Java
    Par Gouasmi_moh dans le forum Interfaces Graphiques
    Réponses: 1
    Dernier message: 23/04/2011, 19h00
  2. Réponses: 5
    Dernier message: 03/07/2008, 17h14
  3. [PHP-JS] comment lancer une fonction javascript a partir de php
    Par amika dans le forum Général JavaScript
    Réponses: 4
    Dernier message: 06/06/2005, 20h23
  4. Appel d'une fonction en C par un noyau en asm (link)
    Par julson dans le forum Programmation d'OS
    Réponses: 7
    Dernier message: 22/03/2005, 16h14
  5. Comment lancer une fonction
    Par Elois dans le forum PostgreSQL
    Réponses: 7
    Dernier message: 21/04/2004, 20h10

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo