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!
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<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
Code Block
<SPAN class="keyword token">import</SPAN> subprocess <SPAN class="keyword token">from</SPAN> subprocess <SPAN class="keyword token">import</SPAN> STDOUT <SPAN class="keyword token">def</SPAN> <SPAN class="token function">run_script</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> params <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>r<SPAN class="string token">"path_to_matlab.exe"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"param1"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"param2"</SPAN><SPAN class="punctuation token">]</SPAN> msg <SPAN class="operator token">=</SPAN> subprocess<SPAN class="punctuation token">.</SPAN>check_output<SPAN class="punctuation token">(</SPAN>params<SPAN class="punctuation token">,</SPAN> stdout<SPAN class="operator token">=</SPAN>STDOUT<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">return</SPAN> msg <SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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.
run_cmd<SPAN class="punctuation token">(</SPAN>r<SPAN class="string token">"%Folder%"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
<SPAN class="keyword token">import</SPAN> subprocess <SPAN class="keyword token">def</SPAN> <SPAN class="token function">run_cmd</SPAN><SPAN class="punctuation token">(</SPAN>f<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> params <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">"cmd.exe"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"/c"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"dir {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>f<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN> msg <SPAN class="operator token">=</SPAN> subprocess<SPAN class="punctuation token">.</SPAN>check_output<SPAN class="punctuation token">(</SPAN>params<SPAN class="punctuation token">,</SPAN> cwd<SPAN class="operator token">=</SPAN>f<SPAN class="punctuation token">,</SPAN> stderr<SPAN class="operator token">=</SPAN>subprocess<SPAN class="punctuation token">.</SPAN>STDOUT<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">return</SPAN> msg<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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 xyz-xyz 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) <SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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-3d4852609d33&searchIndex=0 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:
Thanks again for contributing and I hope this helps and let us know if you have any questions.
Membros conectados podem postar, seguir atualizações e mais. Novo aqui? Registre uma conta gratuita.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.