How to compile and deploy SimBiology file on the web

Hi everyone. Really need someone to assist me on this matter. I have an *.sbproj file. I know that we need to export the file and use the exported model in order to deploy it in a standalone application. But, how can I use this file for the deployment in the web? I don't have any idea on how to create the *.jar file for this case. One of the example show it this way (for standalone application),
mccCommand = ['mcc -m T1.m -N -p simbio -a exportedM1.mat', dependencies_2];
But since I need it to be in a *.jar file, I don't know how to do it. Hope someone could show me the right way to do this. Any examples will be very much appreciated. For your information, I am using Eclipse Kepler to build Java servlet and JSP and using Tomcat as the server. Thanks in advance!

 Accepted Answer

Hi Zetty,
You need to take two additional steps to update this example to work with MATLAB Builder JA.
First, you need to modify the mcc command so that it produces a Java jar file instead of an executable. You do that by changing the -m option to something like -W java:package,Class. Replace package by the package you want the generated Java classes stored in, and replace Class with the name you want to use for the main class in this package. For example, to create a class named SBApp in a package pkg, you can update your sample code as follows:
mccCommand = ['mcc -W ''java:pkg,SBApp'' T1.m -N -p simbio -a exportedM1.mat ', ...
dependencies_2];
Second, you need to create a Java class that somehow runs the class in this jar file. To learn the details on that, you should inspect the generated Java code and the MATLAB Builder JA documentation. However, here's a simple class that will run the Java class created with the command above, wait until all figures are closed, and then exit:
import com.mathworks.toolbox.javabuilder.*;
import pkg.SBApp;
public class runApp {
public static void main(String[] args) {
try {
SBApp app = new SBApp();
try {
app.T1();
app.waitForFigures();
} finally {
app.dispose();
}
} catch (MWException e) {
e.printStackTrace();
}
}
}

6 Comments

Hi Arthur, thank you so much for your time! By the way, I am really sorry because I really need your help to explain further. The file T1.m stated above is actually the .m file from the GUI .fig created for the standalone application. Can we use this GUI to be deployed in the web? If I can't, how to just run the mcc without the T1.m? Can I just remove it from your example? And if I can, is there any example projects for a GUI that is deployed in a web? I have tried to search everywhere but to no avail. These things are really new for me and I don't really know how to do. Many thanks for your help!
Hi Zetty,
If you need a web-based GUI for the SimBiology app, I think you'll need to build the GUI separately. I'm afraid I don't have any experience with that myself. However, the following MATLAB Answer post describes some of your options and links to some additional resources: http://www.mathworks.com/matlabcentral/answers/97350-how-can-i-convert-my-gui-made-using-guide-into-a-web-application-in-matlab-7-7-r2008b
Owh, I thought the latest version of MATLAB could somehow permit deploying GUI in the web method. ;)
So I have made my simple GUI in html web page. I have tried a few times to produce the .jar file based on the MATLAB documentation and this is the final one which gives less error.
1 sbioloadproject('PK_Model_Example_1', 'm1');
2 exportedModel = export(m1);
3 accelerate(exportedModel);
4 save exportedM1 exportedModel;
5 dependencies_1 = sprintf(' -a %s', exportedModel.DependentFiles{:});
6 mccCommand = ['mcc -W' 'java:runPKmodelSbio,Function' '-N -p simbio -a exportedM1.mat', dependencies_1];
7 eval(mccCommand);
Because I am trying to make a .jar for the PK_Model_Example_1.sbproj, so I remove the T1.m. The error comes out to be like this:
Error using CompileRunPK (line 7)
Error: Unexpected MATLAB expression.
I don't know what else to try. Hope you could give some idea. Thank you in advance for your kind help!
Hi Zetty,
Let me clarify one thing. You can deploy a MATLAB GUI to a Java jar file or an executable. What I don't know is how to get that Java GUI to display in a web browser. It sounds like you may be near the limit of the support that people can provide on MATLAB Answers, so I suggest you contact MathWorks Technical Support if you still have questions after reading the rest of my comments below.
The error you are getting is because MATLAB cannot understand the command you are trying to eval on line 7. I suggest viewing the value of mccCommand before calling eval to make sure you have constructed it correctly.
In particular, you need to insert spaces between the different parts of your command. If you look at the value of mccCommand, you'll probably see it looks something like this:
mcc -Wjava:runPKModelSbio,Function-N T1.m -p simbio -a exportedM1.matC:\Temp\...
Instead, you need to try something like this:
mccCommand = ['mcc -W java:runPKmodelSbio,Function -N -p simbio -a exportedM1.mat ', dependencies_1];
Note the space after .mat. Also note that I have added back T1.m. That's because I think the command will error without it. The way the MATLAB Compiler and the MATLAB Builder JA work are that they require you to deploy one (or more) MATLAB functions. This function is how you actually simulate your SimBiology model. So you need to modify function T1.m so that it loads the model from exportedM1.mat and simulates it.
To summarize, here's how I think things would work once you properly set them up:
  1. A user visits a web page that displays a GUI you created.
  2. When the user interacts this GUI, it communicates with your web server.
  3. This web server loads the Java jar file you created and calls the function T1.
  4. Function T1 simulates the model and returns some data to the web server.
  5. The web server sends the results back to the web browser, and the browser updates the GUI.
Hi Arthur, thanks again! I have actually tried to work backward, where I have exported the SimBiology as equation and save all the equations in a matlab function file. It works great! But I think it's just like a redundant works. But still, your answers help me a lot! Will continue the question with Technical Support. Thanks!
To clarify: MATLAB Builder JA does indeed support showing MATLAB GUIs locally when using a MATLAB Builder JA class in a local Java application. It does not support displaying those GUIs in a browser when the classes are used in a Web Application though.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!