How to properly call system on Windows
Show older comments
I'm trying to make a project cross platform, it already works on Linux systems.
The main problem I am having is that I need to call an external executable qafm / qafm.exe
On Linux I just put the executable on in ~/bin, which is on the PATH for my user, so running
system('qafm --rest-of-arguments')
works nicely.
For instance
system('qafm')
returns 1 and prints usage to the command window, as I'd expect.
However on windows I am having trouble.
First, I don't know how to set a user specific path, so I just copied the executable qafm.exe to a MATLAB folder.
This isn't some broken rpath or anything though, if I run .\qafm or .\qafm.exe in powershell or cmd in that folder I get the expected usage output.
However, if I run in the same folder in MATLAB R2019b
system('.\qafm')
it returns -1.0737e+09 (Not a valid error code for the program I promise) and prints nothing to the command window.
Any idea what the problem might be?
9 Comments
dpb
on 24 Jul 2021
- Build a fully-qualified path name to the installed executable on the system and include that in the system() call...if the executable is properly installed so it knows where it's needed stuff is -- copying just an executable to another directory may not get everything it needs, Or
- Build a .bat file that contains commands to set whatever environment the application needs to run -- then call the .bat file from MATLAB instead.
Kylie MacFarquharson
on 24 Jul 2021
Edited: Kylie MacFarquharson
on 24 Jul 2021
dpb
on 24 Jul 2021
system just spawns the default command processor -- normally on Windows that's CMD, but it starts a new session with the default location and environment; it doesn't inherit anything from the MATLAB session that spawned it.
The .exe will try to run with whatever set of DLLs are in the path at the time -- if you have moved it from its default installed location, then it may well pick up a set from the given installation and not those that were packaged with the application itself.
I'll still bet odds are favorable that if you run the system() command with a fully-qualified filename to the original installed application it will work as desired.
Try just
!cmd
then set at the resulting prompt inside MATLAB and then compare that with the environment from the CMD shell from START. Also try executing the executable from each.
Kylie MacFarquharson
on 24 Jul 2021
Edited: Kylie MacFarquharson
on 24 Jul 2021
Yongjian Feng
on 24 Jul 2021
Set your PATH env var to point to that folder?
Kylie MacFarquharson
on 24 Jul 2021
dpb
on 24 Jul 2021
Hmmm....I just noticed that MATLAB does that same dirty here, too...I almost never use CMD.EXE as I use the JPSoft TakeCommand command processor instead so hadn't noticed MATLAB inserting that when it spawns the shell--that's just rude!
So, use the .BAT file route to circumvent MATLAB doing something not wanted--in the .BAT file you can set the environment as needed and working directory wherever you wish it to be before invoking the app.
YECCCCHHH! I just did a
|getenv('path')
inside MATLAB and discover that the ML install has left the every previously-installed version in the current path...
ans =
...;C:\ML_R2020b\runtime\win64;C:\ML_R2020b\bin;C:\ML_R2019b\runtime\win64; ...
going all the way back to R2014b (fortunately, I only install 'b' releases).
Anyways, I think the solution to your problem is to put your code in its own folder and use a batch file to dispatch it -- then you can control the search path and any other environment issues you need to.
Kylie MacFarquharson
on 24 Jul 2021
Edited: Kylie MacFarquharson
on 24 Jul 2021
dpb
on 24 Jul 2021
That'll work but it seems to me a better way would still be to have the executable on its own rather than adding into a ML directory...but it's your code so your choice on that organization structure.
May as well put it in as an Answer -- that'll indicate to others that watch but may not be around today that there is a solution so that if they're busy don't spend time on Q? that do have at least one acceptable Answer.
As for the other old paths; I grok that; I just would have hoped the older releases would have been cleaned up with the new install -- although I suppose they're needed if one wanted to be able to run older releases as well. I haven't figured out how to keep the license active on them all so while the older installs are still on the system, they're not activated. I suppose if I were to uninstall them, then it would clean things up. But, who knows--I might at some point in the future need one of the earlier releases and have to go figure out the license thingie then... :) As long as "out of sight, out of mind!" I don't guess it hurts too much unless the path reaches whatever its internal length limit is...
Accepted Answer
More Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!