Using ExternalProgram Block with MATLAB

89 views
Skip to first unread message

darby.h...@gmail.com

unread,
Apr 2, 2021, 12:58:55 PM4/2/21
to Jaamsim Users Discussion Group
Hi,
I have been working with the ExternalProgram block to attempt to use MATLAB to perform calculations with multidimensional arrays in my model. I reviewed an example that was posted quite a while ago that ran Python code externally and used the output from that in Jaamsim. I believe the model that I set up is similar, but I am having trouble inserting the input provided in the DataSource field into MATLAB. I have been able to trigger the running of the code in MATLAB, but when it requests an input in the command window nothing is provided. I am not sure whether this is because MATLAB is taking some time to start up, or if the command window is not compatible. Any tips would be much appreciated, I think I am close but missing something. I will attach a version of the sample model I created as well as the basic MATLAB script (simply multiplies the input by 10). 
Thanks,
Darby

darby.h...@gmail.com

unread,
Apr 2, 2021, 1:10:50 PM4/2/21
to Jaamsim Users Discussion Group
MATLAB Compatibility Test 3-31-21.cfg
Matlab_Test_1.m

Harry King

unread,
Apr 2, 2021, 1:12:11 PM4/2/21
to Jaamsim Users Discussion Group
Darby,

I'm not very familiar with MatLab. Does it have the necessary command line functionality?  To work with the ExternalProgram object, it must be able to receive inputs on its command line, perform a calculation, return the result, and close itself automatically on completion. The following MatLab documentation may help: https://www.mathworks.com/help/matlab/ref/matlabwindows.html.

Harry

darby.h...@gmail.com

unread,
Apr 5, 2021, 12:02:27 PM4/5/21
to Jaamsim Users Discussion Group
Hi, 
Yes, it does have a command window similar to python. The input and output for what I wrote are both expressed in the command window. I also do have it setup to close the window upon completion. 

That page is interesting - is it possible to call an external program via the command line, straight from Jaamsim? I looked around at that but could not find a way to use the command line in jaamsim, and it seemed like more of a workaround. If that is possible, is there somewhere I can see the syntax for using the command line to call a program with the externalprogram block? 
Thank you,
Darby

Harry King

unread,
Apr 5, 2021, 12:31:46 PM4/5/21
to Jaamsim Users Discussion Group
Darby,

Using the ExternalProgram object is the correct approach for your application.

With your present inputs, the ExternalProgram object is attempting to launch MatLab using the following command line:

     "/C:/Program Files/MATLAB/R2020b/bin/matlab.exe"  "/C:/Users/houghtond/Documents/MATLAB/Matlab_Test_1.m"  11  11

I don't think this will work, but I can't test it myself without a copy of MatLab. Try launching MatLab in the Windows command prompt using this command. Once you develop a command that works, you can translate it into the equivalent inputs for ExternalProgram.

Harry

darby.h...@gmail.com

unread,
Apr 6, 2021, 1:53:11 PM4/6/21
to Jaamsim Users Discussion Group
Ok I see. I was assuming that the "ProgramFile" field was for the .exe for the program used, and the "InputFile" was for the specific script or program being run (like in this example: https://groups.google.com/g/jaamsim-users/c/hSPA1_nS8U8/m/TUWDTDO1CgAJ ). If I wanted to run this with specific inputs from the command line, such as one that I saw from you link that allows outputs to be saved in a .log file from Matlab, what syntax would I use for that? Trying to simply put some type of command prompt command isn't allowed in the jaamsim input fields.

The command that works as I want it to in the command prompt is: 
C:\Users\houghtond\Documents\MATLAB>matlab -nosplash -logfile valuelog.log Matlab_Test_3.m

However I just can't figure out the syntax to execute this type of command in jaamsim.

Thanks,
Darby

Harry King

unread,
Apr 6, 2021, 3:09:54 PM4/6/21
to Jaamsim Users Discussion Group
Darby,

To get the command line:
C:\Users\houghtond\Documents\MATLAB>matlab -nosplash -logfile valuelog.log Matlab_Test_3.m

Enter the following inputs to ExternalProgram:

ProgramFile keyword:  '/C:/Program Files/MATLAB/R2020b/bin/matlab.exe'

DataSource keyword: { -nosplash } { -logfile } { valuelog.log } { Matlab_Test_3.m }'

These inputs assume that the JaamSim executable is located in your working folder C:\Users\houghtond\Documents\MATLAB

Harry

darby.h...@gmail.com

unread,
Apr 7, 2021, 1:27:42 PM4/7/21
to Jaamsim Users Discussion Group
Thank you. I did not realize that each command line entry should be it's own keyword. That, combined with using a function instead of a script, made it run. I have one more question about the DataSource keyword syntax - how can I make the keywords send an attribute of the object passing through to the external program? I am trying to perform a calculation in MATLAB that uses an attribute of an entity called NUMBER, that could be different for each entity that passes through. The DataSource keywords I tried were:

{ -nosplash } { -batch } { testfxn(this.obj.NUMBER) }

However, this seems to be sending the string "this.obj.NUMBER" into MATLAB instead of the attribute I am trying to reference. How can I reference the correct number in this output, instead of having it treated as a string? I tried variations such as having "(this.obj.NUMBER)" as a seperate keyword, but could not find a syntax that worked here. (Note the above runs as expected if I replace (this.obj.NUMBER) with a value). Any idea how to approach this?

Darby

Harry King

unread,
Apr 7, 2021, 3:22:26 PM4/7/21
to Jaamsim Users Discussion Group
Try the following:

{ -nosplash } { -batch } { 'format("testfxn(%s)", this.obj.NUMBER)' }

Basically, you need an expression that returns the desired string.

Harry
Reply all
Reply to author
Forward
0 new messages