Why do I receive the error "Unhandled Exception: System.IO.IOException: The handle is invalid." when executing an operating system command from MATLAB?

4 views (last 30 days)
I am trying to execute an operating system command from the MATLAB command line that runs my executable, however when executing the following command using the ! operator in MATLAB:
!myProgram.exe
I received the following error in MATLAB:
Unhandled Exception: System.IO.IOException: The handle is invalid.
I also receive this error when using the "system" function instead with the following syntax:
system('myProgram.exe')
I am able to run the executable by entering the executable in my operating system command prompt directly. So why do I get the error when trying from MATLAB?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 19 Sep 2023
Edited: MathWorks Support Team on 19 Sep 2023
This error is due to the the program you are trying to run which opens a GUI or a separate window to show results. So for this use case, you need to include an & to the end of your commands. Therefore it would look like one of the below:
!myProgram.exe &
or
system('myProgram.exe &')
This requirement is mentioned in both the "Run External Commands, Scripts, and Programs" and the "system" documentation pages, these are linked below:
1. Run External Commands, Scripts, and Programs
2. "system"

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!