Sending SIGINT from within a MATLAB GUI - matlab

From within a MATLAB GUI application, I'm starting an external program (a console application on Windows) that takes care of reading data from a measurement system. The data is stored in several files that are processed by the MATLAB application as soon as the external program has finished. The problem is this:
The external program, when run on the command line, can be gracefully stopped by issuing a SIGINT (i.e. by pressing Ctrl + C). A signal handler traps the SIGINT and shuts down the program. Is there a way to do this from within the MATLAB GUI application, by pushing an "abort" button?
After many hours of searching I stumbled upon http://www.caam.rice.edu/~wy1/links/mex_ctrl_c_trick/ which shows how to detect SIGINT in a MEX file. Letting a MEX call the external program might work (although I'm not sure about the details yet). However, it still requires Ctrl + C to stop the program. How can I send the SIGINT via push button in my GUI?

If you start your external program in Cygwin, then Cygwin will give it a PID. Using this PID you can use Cygwin's kill command to send signals to the process. So start the program from Cygwin. In MATLAB you can use !ps (where ! means call external shell command) to get a list of Cygwin PID's and then !kill -s signal pid to send a signal to the program. To make it happen from a MATLAB GUI let the callback from some button call !kill.

Related

MATLAB code break

I have started running a script on MATLAB that takes days to finish. Usually, if I changed my mind and I don't want wait for it to finish and I get content with the intermediate results, I highlight the command window and press Ctrl-C to break the code.
Now, I have run MATLAB. But its desktop got kinda stuck in the background. When I try to restore the desktop from the toolbar, it does not restore. But I know from the task manager that the process is running and is consuming Memory and CPU performance. So, I am kinda stuck. I don't want to kill the process because I need the intermediate values in the workspace, and I can't open the desktop to break the code using ctrl-c.
Is there any solution? For example, is there any command that can be used in the command prompt to act as ctrl-c for MATLAB?
I am using MATLAB R2012b and Windows 8.
Quick try to fix the recent Problem:
Try ty set a higher priority to matlab.exe in the Task Manager. (Right click -> Priority -> Higher than normal). Then see if you can get the window to front.
Some approaches to avoid this problem in future:
Try to optimize your code. For starters look at: http://de.mathworks.com/help/matlab/matlab_prog/vectorization.html
Use Matlab compiler for faster execution: http://de.mathworks.com/products/compiler/
Include some drawnow commands at stratetic positions in the code. This allows matlab to process the Event Queue and capture ctr-C commands.
Save intermediate results to output files. For example you could write an output file all 30 min with your intermediate results. Easyiest way would be just save(filename). Then a .matfile with all your workspace variables is generated. You can than kill the process in the task manager, without loosing too much results.

Blocking Ctrl-C / Ctrl-Z in an input command

In Matlab or GNU Octave, this command input("") allows to break the input process by pressing CTRL+C or CTRL+Z. Is there a way to block this, so that only a valid input is accepted?
Note: Pressing CTRL+Z on an input command even exits the Octave shell in a Linux terminal, so I understand that the control must be established at a different layer.
So you want input to ignore when Ctrl + C is pushed? I don't think that's possible simply because this could potentially be dangerous. Ignoring listening to Ctrl + C could open the possibility of your code running endlessly and you not being able to break out of the code unless you manually kill MATLAB. I'm not saying that what you desire with input will be dangerous, but if MATLAB allowed you to ignore this keystroke in any capacity, then you'd have that dangerous situation that I talked about if used improperly.
FWIW, check out this link on MATLAB Central: http://www.mathworks.com/matlabcentral/answers/25213-disabling-ctrl-c-in-a-gui . Here, someone is trying to ignore Ctrl + C so that their GUI doesn't needlessly crash, but MathWorks confirms that it is not possible to do so.

How to return to prompt after running an exe file compiled with mcc (Matlab compiler)

I have an executable created with mcc. The .m file has a simple function that reads and plots values. After I run it from DOS, it freeze without returning execution to DOS. 2 questions:
1) How can i return execution to dos? I tried "return" and "exit" commands but didnt help
2) How to close the dos windows? is the only way to use a batch file or can I do it with a command in the .m file?
thanks
A.
There are 2 scenarii:
If your run your matlab executable from a DOS window, the DOS window will not get control back until the program terminates. If the program generate matlab figures (plot, surf, etc...), the program will not return to the console until all of the figures are closed.
You may think it is a waste for a simple plot, but after all your figure could be a evolved gui with a lot of code to execute. Or even a simple figure with a closeRequestFcn. So in Matlab terms, your program may still have instructions to execute as long as a figure is opened, so it will not return until it sure there is nothing more to do.
If you simply double clicked on your executable, the DOS looking console which open with your program will have the same behaviour. It will not disappear until the program returns (so until all your figures are closed if relevant).
I am not sure about linux, versions, but if you are running on windows, there is a way to suppress the DOS looking console for your graphic applications. Look at the -e switch in the mcc options.
This switch will compile your program in a way that no DOS console is opened when you double click on your executable.
So to summarize, I would recommend:
If your program is a 'command line' type (a function that takes input from the console and return values to the same). => Compile with normal options, and execute it from a DOS window (you do not want the window to disapear as soon as the program terminates.)
If your program is a gui or even simple plotting functions, with no need for console interactions, then compile with the -e switch and execute it by double clicking the .exe file.
note that in case you use the -e switch, it is recommended to direct potential output to a logfile. Look at the mcc documentation for more info.
edit
If you really need the DOS console and some graphic output, run your program form the command window with the following syntax:
start /b YourProgram
This will start the program in "background mode" (use YourProgram & in Linux terminal). You will be able to do anything in this console window, and you will also see the output from your matlab executable.
It can be confusing because the output from your program will be added to the simple dos prompt and you may think you do not have the control but if you type any command it will work. You can even start many program this way and retain control in your console, but all the output will arrive in the same window and they may be difficult to differentiate.

Execution of an external program in MATLAB - succeeds only when using &(ampersand), but then does not wait for completion

I am attempting to execute an external program from Matlab:
cmdstr = sprintf('"%s\\myEXECUTABLE" "%s" -options',fullEXEpath, fullInputFilePath);
[status, res] = system(cmdstr);
I receive "status = 1", partial program output in "res" (though no error message) and no output files.
BUT, executing exactly the same command with & (ampersand):
cmdstr = sprintf('"%s\\myEXECUTABLE" "%s" -options &',fullEXEpath, fullInputFilePath);
[status, res] = system(cmdstr);
Meaning in the background via a dos command window, works just fine (status equals 0 and output files are created).
I have seen somewhere it might be that the antivirus is blocking the program from executing via Matlab, but I cannot disable it since I am an endpoint user.
Using "&" causes my GUI to open a command window and run in the background, while immediately resuming Matlab code.
I can live with the command window opening, but not with Matlab resuming right away, as I use the output files in my MATLAB code, which are not necessarily ready.
Is there a way to verify the external program has ended?
I tried simply:
while (status) %waiting for system to return status = 0
disp 'waiting...';
end
but it seems to still return with "status = 0" before completion...
Or rather - is there a way to avoid the &?
Any answer will be much appreciated.
as a debugging method, you can use
system(cmdstr,'-echo')
Since the error messages have nowhere to go, you wouldn't see them. (matlab only returns the output, which might not contain the error stream)
Am I correct you are calling a GUI program with that command? According to the matlab documentation:
The ampersand, &, character has special meaning. For console programs
this causes the console to open. Omitting this character causes
console programs to run iconically. For GUI programs, appending this
character causes the application to run in the background. MATLAB
continues processing.
So console programs (headless programs) would not allow Matlab to continue while executing.

matlab compiled program execute code on close

I have a compiled matlab program that runs as a dos box basically (ie no GUI). Is there a way to get code to execute when someone closes the program? Specifically closing by clicking on the X?
I suggest to define a cleanup task. This will be executed whenever a function is terminated (by whatever means), so as long as closing the program is not done with a kill -9 command or similar, the code is being run.