How do I call a Matlab script with Model Builder?

868
2
05-15-2017 11:45 AM
TaufiqRashid
Deactivated User

I have some Matlab scripts which I want to use in ArcMap. Is there any method to use Matlab scripts with model builder in ArcMap? Thanks!

0 Kudos
2 Replies
TimothyHales
Esri Notable Contributor

Hi Taufiq - Thanks for asking your questions and welcome to the GeoNet community! I wanted to let you know that we're moving your question from the GeoNet Help group to the https://community.esri.com/groups/model-builder?sr=search&searchId=500696b4-eaae-4957-83cd-3d4852609... space so our Esri and user subject matter experts can further help answer this and future questions.  So you're aware on how and where to post your questions, here's a few quick tips and reminders:

  1. First, use the GeoNet search (top right corner by your profile icon) to search and see if your question has already by asked/answered previously.
  2. If not, than we suggest reviewing the GeoNet Community Structure, to find the most relevant space to post your question. 
  3. The GeoNet GeoNet Help group is for questions and tips on how to use the GeoNet community platform not the Esri ArcGIS platform. Following steps 1 & 2 above will give you the best results for getting Esri product questions answered.
  4. This GeoNet 101 "How to Ask Questions on GeoNet" post is also a helpful guide to asking questions: https://community.esri.com/community/help-and-feedback/blog/2016/08/15/community-news-and-tips-how-t...

 

Thanks again for contributing and I hope this helps and let us know if you have any questions.

curtvprice
MVP Esteemed Contributor

I am not familiar with MATLAB at all. However if you  know how to run a MATLAB script from a command line, you can run a command line from Python using the Calculate Value tool.

python - Executing a subprocess fails - Stack Overflow 

UPDATE the above example has some good suggestions but is very dated. Updated below, based on the current Python docs:

17.1. subprocess — Subprocess management — Python 2.7.13 documentation 

For example

Expression

run_script()‍‍‍‍‍‍‍

Code Block

import subprocess
from subprocess import STDOUT
def run_script():
    params = [r"path_to_matlab.exe",
             "param1",
             "param2"]
    msg = subprocess.check_output(params, stdout=STDOUT)‍‍‍‍‍‍‍
    return msg ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Here's a bonehead example of running and command line to get a folder list from cmd.exe.

In the example, the model element Folder is of type Folder, created with right click, Create Variable.

Expression

run_cmd(r"%Folder%")‍‍

Code Block

import subprocess
def run_cmd(f):
  params = ["cmd.exe", "/c", "dir {}".format(f)]
  msg = subprocess.check_output(params, cwd=f, stderr=subprocess.STDOUT)
  return msg‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

When this is run in Model Builder, I get this:

Executing (Calculate Value): CalculateValue run_cmd(r"C:\Users\cprice\Documents\ArcGIS") "import subprocess\ndef run_cmd(f):\n  params = ["cmd.exe", "/c", "dir {}".format(f)]\n  msg = subprocess.check_output(params, cwd=f, stderr=subprocess.STDOUT)\n  return msg" Variant
Start Time: Wed May 24 15:29:16 2017
Value =  Volume in drive C has no label.
 Volume Serial Number is XXXX-XXXX

 Directory of C:\Users\cprice\Documents\ArcGIS

05/05/2017  11:07 AM    <DIR>          .
05/05/2017  11:07 AM    <DIR>          ..
11/18/2016  03:03 PM    <DIR>          AddIns
03/03/2017  09:55 PM             1,123 arcpy_raster.py
(...)
              38 File(s)    949,426,908 bytes
              23 Dir(s)  23,970,992,128 bytes free

Succeeded at Wed May 24 15:29:16 2017 (Elapsed Time: 0.11 seconds)
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍