I'm trying to run .exe code inside Matlab terminal (R2018a) but I'm encountering some problems.
My .exe code runs normally on Windows cmd with the following generic command (an expected output file is created)
code.exe < input
In Matlab I tried 4 approaches:
system('code.exe < input');
dos('code.exe < input');
system('code.exe < input', '-echo');
system('set path=%path:C:\Program Files\MATLAB\R2018a\bin\win64;=% & code.exe < input');
They are returning the line below and no output file is created.
ans = -1.0737e+09
Adding the "&" character at the end of the line of attempt 1, as sugested by ref and many others, Matlab opens the Windows cmd in the correct folder but does not execute the .exe code properly (no processes are running in task manager), no output file is created, e.g.:
system('code.exe < input &');
I also tried the suggestion of this reference .exe run in matlab does not create two output files like it does when run in command prompt but I was not successful. This reference suggestion is also returning and no output file is created: "ans = -1.0737e+09"
Edit:
According to this link, user Walter Roberson says "-1073741511 is hex C0000139 which appears to correspond to the Windows error code for "Entry point not found". That indicates that you either tried to execute something that was inherently not properly created (such as if you tried to directly execute the DLL that did not have a main program), or else that the program you executed tried to use a DLL that could not be found."
Any insights on how to resolve this? Thanks.
The answer from this link solved my problem.
Basically, You will have to create a batch file similar to the one below and change the Matlab path according to your version.
fid = fopen('myBatchFile.bat','w')
fprintf(fid,'%s\n','set path=%path:C:\Program Files\MATLAB\R2018a\bin\win64;=%');
fprintf(fid,'%s\n','code.exe < input')
fclose(fid)
system('myBatchFile.bat')
Related
I installed an application named lqns in the path: /home/robb/Research/dist/lqns-6.2/lqns (lqns is a folder containing the executable lqns). I want the program to be executed in command line simply calling lqns in the shell, I solved this adding to the file ~/.bashrc the line:
export PATH=$PATH:/home/robb/Research/dist/lqns-6.2/lqns
And it works with no issue. I am now trying to execute this program inside a Matlab script, running:
[status, ~] = system("lqns " + filename, '-echo');
Where filename is the path of an input file. I get the error message:
/bin/bash: line 1: lqns: command not found
Running the exact same command with the shell I get no error: the program runs with no problem generating the relative output.
Running getenv('PATH'); in Matlab and printenv PATH on my OS shell I indeed get two different results: Matlab does't have the path to lqns. I even tried editing manually the files /etc/environment, /etc/bash.bashrc and /root/.bashrc, with no result. How can I solve this issue?
you need to launch matlab by typing matlab in a terminal, not by double clicking on its shortcut from your desktop. (or even typing ./matlab in a terminal from your desktop)
it's up to the operating system to determine what double clicking does, and it's not guaranteed to execute most of your shell initialization scripts (or even launch it from the correct shell to begin with).
more info at Why are environment variables not resolved when double-clicking .desktop file?
I have a startup Matlab script startup.m under
C:\SVN\myscripts\StartupScript
The script sets up paths and does some subsequent script calling successfully.
However, i created another file callMat.m in the same folder that just displays some data. When i run it from Matlab GUI, it works fine.
Howeever, calling it from cmd.exe,like below:
C:\Program Files\MATLAB\R2013b\bin\matlab.exe" -nosplash -nodesktop -wait -r "callMat"
I found that it opens Matlab command line and then
it first executes startup.m and then callMat.m .
Is this expected behavior, and if yes Why?
Thanks
sedy
Yes, this is expected behaviour. MATLAB executes startup.m when it starts up, whether it's started in the usual way, or whether it's started from the command line with -r and a command.
startup.m on your Matlab path is executed on startup.
I can find no description on the Matlab website suggesting that command line starting is any different.
In conclusion, it seems very likely this is expected behaviour. The documentation page here gives some suggestions as to the different places to put different options. In particular, it suggests that you:
Use startup.m to modify the default search path, predefine variables
in your workspace, or define defaults for graphics objects.
Is there a way of running a MATLAB script from Notepad++?
Obviously I have MATLAB installed on my computer. I know you can set a path for Notepad++to run when you hit F5, but when I set this path to my MATLAB.exe file, it simply opens another instance of MATLAB.
This is not what I want, I want the actual script in Notepad++ to be executed in the already open and running instance of MATLAB.
I'm afraid I'm not on my home computer at the moment to test this out, so the following is just a suggestion for you to try.
If you take a look at the NppExec plugin for Notepad++, you'll see that with it you can specify a command to be run when you hit F6 (like an enhanced version of hitting F5 in the regular Notepad++). You can also give it variables such as the path to the current file, and the name of the current file.
MATLAB (on Windows at least - I assume you're on Windows) makes available an API over ActiveX/COM. If you search in the MATLAB documentation for details, it's under External Interfaces -> MATLAB COM Automation Server. By running (in MATLAB) the command enableservice('AutomationServer') you will set up your running instance of MATLAB to receive instructions over this API.
You should be able to write a small script (perhaps in VBScript or something similar) that will take as input arguments the path and filename of the current file in Notepad++, and will then connect to a running instance of MATLAB over the COM API and execute the file's contents.
Set this script to be executed in NppExec when you hit F6, and it should then run the current file in the open instance of MATLAB.
As I say, the above is just speculation as I can't test it out right now, but I think it should work. Good luck!
Use NppExec add-on and press F6, copy paste the following and save the script:
NPP_SAVE
set local MATPATH=C:\Program Files\MATLAB\R2015a\bin\matlab.exe
cd "$(CURRENT_DIRECTORY)"
"$(MATPATH)" -nodisplay -nosplash -nodesktop -r "try, run('$(FILE_NAME)'),
catch me, fprintf('%s / %s\n',me.identifier,me.message), end"
then run (press F6; enter). Matlab Console and Plot windows still open and stay open. Error messages will be displayed in opening Matlab command window. Adding
, exit"
to the last command will make it quit and close again. If you want to run an automated application with crontabs or the like, check Matlab external interface reference for automation.
matlab.exe -automation ...
Also works in cmd terminal, but you have to fill in the paths yourself.
This is a usable implementation upon Sam's idea. First, execute MATLAB in automation mode like this.
matlab.exe -automation
Next, compile and execute this following VB in NppExec plugin. (which is to use MATLAB automation API)
'open_matlab.vb
Imports System
Module open_matlab
' connect to a opened matlab session
Sub Main()
Dim h As Object
Dim res As String
Dim matcmd As String
h = GetObject(, "Matlab.Application")
Console.WriteLine("MATLAB & Notepad++")
Console.WriteLine(" ")
'mainLoop
while True
Console.Write(">> ")
matcmd = Console.ReadLine()
' How you exit this app
if matcmd.Equals("!!") then
Exit while
End if
res=h.Execute(matcmd)
Console.WriteLine(res)
End while
End Sub
End Module
Then you'll get a matlab-like terminal below your editor. You can then code above and execute below. type !! to exit the terminal.
What it looks like
Tips: don't use ctrl+c to interrupt the MATLAB command, because it will kill the whole process instead.
I have a executable that when double clicked opens in a command line window.
Now there is a input file (i.e named "sphere_15000.inp") in the same directory where the executable apame_win64.exe is located. So we can inter the file name in the command line.
The question is how this can be done from mathematica front end? With the RunThrough command I tried to do it with no avail.
RunThrough["Executable Location", "sphere_15000"]
Do I need to put this file location in my Windows 7 environment path variable? Hope for some suggestion in this regard.
UPDATE
Found a solution to my problem.
First set the Mathematica directory to the folder where the executable is located.
path="C:\Users\FlowCrusher\Desktop\CUSP solver\Apame_build_2011_01_09\solver";
SetDirectory[path];
Then use the following style of input.
Run["\"\"apame_win64.exe\" \"input\"\""]
Here "apame_win64.exe" is the executable one want to run and "input" is the input file for the executable. This solves the problem. But a new item in the wishlist.
Is there a way to hide the console window in the background?
Here is how it looks on my screen.
As Chris suggested if we use minimized console in the Run command we get a minimized window but the program does not execute.
I hope that a solution exists.
BR
Yes, you might put the folder of you executable into the Path variable, or provide the full path name.
However, RunThrough seems to have been superseeded (on Windows) by
Import["!command ","Text"], which will execute command and read the comaand line output into Matheamtica as a string.
E.g.:
Export["testit.txt", "bla", "Text"];
Import["!dir" <> " testit* > dir.log", "Text"];
FilePrint["dir.log"]
--
Otherwise, I also had good results in the past using NETLink (not sure if WScript.shell
still works on Windows7/8 or if one should use something else).
Emulating Run (RunThrough is not really needed I think):
Run2[cmd_String] := Module[{shell},
Switch[$OperatingSystem,
"Windows",
Needs["NETLink`"];
shell = NETLink`CreateCOMObject["WScript.shell"];
shell # run[cmd,0,True],
"Unix",
Run # cmd,
"MacOSX",
Run # cmd ] ];
Can you run your application with input from a basic command window instead of the application console? This might be the form of command you would need:
apame_win64 -input sphere_15000.inp
or simply
apame_win64 sphere_15000.inp
You can probably check the available switches by running apame_win64 -help
A multi-part command can be run from Mathematica, e.g.
Run["type c:\\temp\\test.txt"]
Alternatively, also returning output to the Mathematica session:
ReadList["!type c:\\temp\\test.txt", String]
I landed here wanting to run abaqus command line on windows.
The solutions provided here worked out for me (Windows 7, Mathematica 9):
SetDirectory#path;
Run["start /min abaqus job=" <> fileName <> " interactive ask_delete=OFF >> log.txt"]
(Here the abaqus option ask_delete=OFF overwrites an existing simulation results and the >> redirects all the output to a file)
I think, minimizing the window did not run in your case since the executable throws open that window. In that case, this might be of some help
I am using a trans.exe file, which when run asks for a parameter (=input) file. If I run trans.exe using Matlab, then how can I directly give the parameter file inside the program without being prompted by Matlab to type it manually each time trans.exe is run?
If your executable doesn't have the ability to accept command-line parameters, then your only option is to invoke a call which pipes stuff to the stdin of your executable (under Linux, this would be something like !echo "blah blah blah" | my_executable). I don't know if this technique works from Matlab, though.
system('"C:\path_name\trans.exe" < "C:\path_name\input_trans_parameter_file.txt"');
The following command line used in above system function directly uses the name of the input file stored in input_trans_parameter_file.txt.
< "C:\path_name\input_trans_parameter_file.txt"