How to execute command line calls (JAVA methods) from within Simulink

5 views (last 30 days)
I would like to execute a piece of code from within Simulink that is similar to calling a function at the Matlab command line. I have initialized a Simulink model to have a JAVA object in its model workspace which has several methods available. Utilizing the object and methods in MATLAB is trivial, but I am struggling to integrate them in Simulink. A MATLAB function block is similar in concept, but I don't think it will allow me to pass in the data type of the JAVA object. If I use an Interpreted Matlab Function, I think this will work, but the method I want to use does not return a number, so it crashes. I think it would be simple to change the JAVA code to return a value, but this is not ideal behavior. Is there a block in Simulink that will simply evaluate a chuck of code and not require that something be output? Similarly, to get a value from the JAVA object, I can use a constant block with the method incorporated and then use a MATLAB function to update the model: "set_param('JavaTest', 'SimulationCommand', 'update')" in order to dynamically evaluate the method contained in the block. So fundamentally, Simulink will evaluate JAVA methods, but I am struggling to find a block or blocks that do exactly what I'm looking for. From my limited experience with S-functions, I believe these have the same limitation of passing in numerical data types. I could recreate all of the JAVA code in an S-function, but this bypasses my whole point of using a JAVA library from within Simulink, so doesn't answer my question. I feel like this should be trivially easy, and I'm just missing a block that's called "command line" or the like. I suppose this has more to do with how Simulink compiles and routes information, and Simulink wouldn't know what to do with such a block. Thank you for any help in advance.
  1 Comment
Joseph El Youssef
Joseph El Youssef on 29 Jan 2014
I am also interested in obtaining more information about incorporating Java objects into Simulink for the purposes of simulation running. Could you provide more information about the embedded code that you use, Andrew? I am looking to create an instantiated object of a java class whose states need to persist throughout the simulation runs. Any help would be appreciated.
Thank you. Joseph.

Sign in to comment.

Answers (1)

Andrew
Andrew on 22 Jan 2014
The guys at Mathworks helped me solve this. It's all about the scope of functions. Essentially, create an embedded Matlab function in the Simulink model which calls a Matlab function. The Matlab function makes use of a persistent or global JAVA Object and that function calls the methods of the object.
The Simulink function might be something like:
coder.extrinsic('callJavaFunction') callJavaFunction('argument')
The Matlab function would then be:
function callJavaFunction(input) global MyJavaObject MyJavaObject.setValue(input)
The Java Object would be created in the base workspace at some point during an initial call, and then the simulation would update its properties.

Categories

Find more on Simulink Functions in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!