IT is better to use the the feval () method () Method. This gives you
Output as an Object and you can try-catch the call.
Object out;
MATLAB MATLAB = New Matlab ();
Try {
OUT = matlab.feval ("sin", new object [] {new double [] {17}}, 1);
} catch (Exception E)
System.out.println ("Error!");
}
The Syntax of The Feval Method Is as Follows:
Argument 1: a string giving the name of the function
Argument 2: an Object Array Where You Pass The Inputs To The Function.
The Same Convertion Rules That Are Co. Calling
Java Methods on The Matlab Command Line Apply Here.
Argument 3: an integer specifying the number of outputs from the number of output
Function.
The Output Is Returned As an Object That You Can Cast To THE
ApproPriate Type to Pick Up The Output.
IF you only want to do a simple evAl in matlab you could pass the
String That You Want To Eval To a Function That Only Has One Line
That Performs an evAal of the string.
In matlab:
Function result = myeval (expr)
Result = evr (EXPR);
In java:
Object out;
MATLAB MATLAB = New Matlab ();
String expr = "sin (17) * COS (0.5667) / 67";
Try {
OUT = Matlab.Feval ("Myeval", new object [] {expr}, 1);
} catch (Exception E)
System.out.println ("Error!");
}
I Need to Call Matlab from Java, Run Some Programs in Matlab and get the standard output.
I know i can Start Matlab with:
Process proc = runtime.getRuntime (). EXEC ("matlab");
But Then How Do I Run A Program in matlab? (For example, if i have "myprogram.m", I would ing it in matlab by type "myprogram")
If i start a new process [process proc2 = runtime ... exec ("myprogram")], IT Does Not Start In Matlab.
Any Ideas?
Thanx in advance.
Sounds Like MyProgram Is Just A Parameter of a Call To Matlab.Try Some Variations ON
Exec ("Matlab MyProgram");
.............. ..
/
What I DID IS: CREATE GUI IN JAVA and Run IT from Matlab Command Line and Force Matlab To Gather Data from GUI BY SETTING A FLAG in Java - All Calculation WERE DONE IN MATLAB.
And the Comes Another Problem: The IS Only One Possibility To Send Anything from Java To Matlab: Using A Loop In Matlab Checking The Java Application If It Finished Preparing Data To Calculate
I don't Remember The Exact Code, but if you insist i could give you some samples.
IN META-CODE It Looks Like this:
Matlab:
MyApplication = my_java_gui_class
While (True)
{
IF (myApplication.readytotakeParameters ())
{
Some_Array = myApplication.readparameters ();
Calculate_sth_in_matlab
Do_something_with_Results_ie_show
myapplication.clearflag ()
}
Sleep (1) #let user think ends
}
Java
Class my_java_gui_class
{
Boolean ReadytotakeParameters = false;
Constructor ()
Public Boolean ReadytotakeParameters ()
{
Return ReadytotakeParameters;
}
// Action Made by Pushing a Button All_Parameters_given (or Pushing Enter Key By User)
Public SomeMethod (Action)
{
IF (Action) ReadytotakeParameters = true;
}
Public clearflag ()
{
ReadytotakeParameters = FALSE;
}
}
I don't know what has change in 6.1, So Check It First.
Summing Up:
When I do Next Project with Mathematical Calculation, I will use pure java with some
Mathematical Functions and Algorithms Written in Java. You Write IT by Yourself or Download It Somewhere from Web. I do not Recommend USING MATLAB WITH JAVA.
/
Artful_bobster,
There are a few ways to call Matlab from Java programs:.! 1) Use the com.mathworks.jmi.jar api, this is undocumented and unsupported by mathworks Plus, as far as i can tell it doesnt work The only thing that ive Had Happen When Ive Tried To Use Simple Matlab Commands (Like Pwd) Is That It Throws A Matlabexception (WhatVer That is!)
2) Use UNIX Scripts to transfer text files containing your value / parameters Between your java app and your instance of matlab. I Didnt Like this IDEA!
3) Use the matlab compiler to compile your mfile functions That You Want to Call, as dlls (or wherever) and the interface with the the the jni (http://java.sun.com/docs/books/tutorial/native1. 1 /). The disadvantage with this is is means that your program will be tied to the platform that you compile the mfiles on. This is the method that mathworks recommend on their website, and i think that it works (it is what im trying To do at the moment.
4) I Have Also Found Today:
(http://matlabdb.mathematik.uni-stuttgart.de/download.jsp?mc_id=9&mp_id=163)
Which provides Ways of calling matlab from java.
Hopefully MathWorks Will Get Their Fingers Out and provide Better Connectivity To Java (Like To to C) in Future Versions of Matlab.
I Hope That Sests Arefi Some Use To People, Because ITS Taken Me Ages To Find A Way To Do this!
Alex
/
The article I translated
/
have written a program in Matlab which creates a matrix. I now need to use this matrix in my Java code but i dont know how to do this. One suggestion I was given was to save the Matlab matrix as a binary file (easy) and THEN Load The File in Java. Does Anyone Know How I Could Do this? And is There Better Ways? I Think There Ways of Running Matlab Code from Java and Vice Versa But I Dont Know What That Involves.thanks.
Well, OX
1) Use the runtime.exec () Method to Execute The Matlab Program
. 2) Get the inputstream from the process object what the exec () Method generates.
3) Modift the Matlab program to write the matrix in some format to the standard OutputStream of the Matlab program - for formating, you can take a row and print it to the OutputStream as a LWS separated line of text terminating with a CR-LF
4) Read The Text Line from The InputStream in The Java Program.
5) Parse The string (May Be with a stringtokenizer) and construct the matrix in a 2d array or by some other means.
Hope IT Helps!
Ironluca
//
1st way to do what, Either Run the matlab Into the jvm, so you need some jmatlab package to do so, i do know and don't think.
2nd way to do that, using java.lang.Runtime, launch Matlab, the real one. The problem with this method is that the interaction with the matlab process is reduced to stream out / err / in communication until Matlab have some kind of Matlabserver Which again I'm pretty Sure It Doesn't Support Right Now.