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

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.

Related

How can I run Matlab .m file repetitive in background?

I have project that was wrote in MATLAB. It has Main file like Main.m, I want to run Main.m repetitive every 1 Second in background.
I don't want to see any display and opening windows from MATLAB.
How can I do this ?
There are two steps to achieve this. First write some m script which calls your Main function every 1s. You can use a loop like this one. Getting the time via toc is important in case your main function takes some time to compute. An alternative are timers, which avoid any time drift (The loop is typically slightly above 1s, the timer will be 1s on average).
Once your MATLAB knows what to do, the question is who to start it. There is the -batch option:
Execute MATLAB script, statement, or function non-interactively.
MATLAB:
Starts without the desktop
Does not display the splash screen
Executes statement
Disables changes to preferences
Disables toolbox caching
Logs text to stdout and stderr
Does not display dialog boxes
Exits automatically with exit code 0 if script executes successfully.
Otherwise, MATLAB terminates with a non-zero exit code.
statement is MATLAB code enclosed in double quotation marks. If
statement is the name of a MATLAB function or script, do not specify
the file extension. Any required file must be on the MATLAB search
path or in the startup folder.
Use the -batch option in non-interactive scripting or command line
work flows. Do not use this option with the -r option.
To test if a session of MATLAB is running in batch mode, call the
batchStartupOptionUsed function.
Example: -batch "myscript"
This means MATLAB will not open any window, instead you see any output in the calling command line. How it looks on LINUX:
x#y ~ $ matlab -batch "1+1"
ans =
2
Assuming there is a top-level function in Main.m that you would like to execute once every 1 second, one possibility is to start an instance of Matlab and create another script that calls your function in a forever loop with a 1 second pause (making sure that this other script has Main.m file in the PATH so it can see it)
function run_main_forever()
while true
my_function()
pause(1)
end
end
You can have a .bat file start matlab in the background and run the script like this:
matlab -nodesktop -nosplash -r "cd('C:\Path\To\'); run_main_forever();"
See this link for further details on launching MATLAB without the desktop: https://blogs.mathworks.com/community/2010/02/22/launching-matlab-without-the-desktop/

Using vim as default editor for matlab

I want to use gvim as the standard editor for Matlab. It used to work on Linux but now I am forced to use windows and I can't seem to figure out how to set the editor such that files are opened in gvim in a new tab.
In the preferences there is a field which allows to pass a command that points to the prefered text editor. That works, but things fail when I try to give additional options, in my case that would be "--remote-tab-silent" to tell gvim to open the file in a running instance in a new tab. More specifically, the following line in the matlab preferences works:
C:\pathtovim\gvim.exe
while this one fails
C:\pathtovim\gvim.exe --remote-tab-silent
A command line opens with the following error message (my own translation from German):
The command ""C:\pathtovim\gvim.exe --remote-tab-silent"" is either spelled incorrectly or could not be found.
My guess is that it has something to do with the additional quotes, I have no idea why the command is issued with quotes, even though in the field I put it without. The follwing commands work when typed into the command line directly:
"C:\pathtovim\gvim.exe"
C:\pathtovim\gvim.exe --remote-tab-silent file.m
and this one fails:
"C:\pathtovim\gvim.exe --remote-tab-silent file.m"
I'd really appreciate any help! Thanks!
I can't find a good way to hack around it through the MATLAB settings; it looks like MATLAB is stupidly expecting the text editor to take only file names as arguments.
I think your best option, is to create a .bat script that simply passes any arguments it receives on to Vim, inserting the --remote-tab-silent.
I.e. create a .bat file with these contents:
"C:\pathtovim\gvim.exe" --remote-tab-silent %*
Then set up your MATLAB preferences to invoke the .bat file rather than Vim.

How to debug matlab code without gui

I have recently started using MATLAB without GUI by starting matlab with -nodesktop option and it is considerably faster.
However presently I have no way to debug a .m script in non gui mode. I have to open the default matlab editor every time I have to debug.Has anyone figured out a way to do it?
Thanks in advance
I am using Ubuntu Linux, in case that helps.
To set breakpoints with the command line, dbstop is the tool (plus dbclear to clear breakpoints and dbstatus to list them).
There are presently 17 different forms to dbstop, which allow you to specify various combinations of:
The M-file in which to stop
Line number
Sub-function
Conditional to an arbitrary expression. For example,
dbstop in myFun.m at 224 if ~exist('x','var')
At any run-time error (dbstop if error)
At a specific error (e.g dbstop if error myFun.m:barErrorId)
At any warning (dbstop if warning) or specific warning
If NaN or Inf are encountered (dbstop if naninf)
See the documentation for dbstop for details and good examples.
Also get used to dbcont (or F5), dbstep (or F10), dbquit (Shift+F5), dbstep (also dbstep in, dbstep out), dbstack (to see where you are and how you got there). The keyboard shortcuts may be different outside of Windows.
Far less used, but still very useful are dbup and dbdown, which allow you to switch workspace context (memory stacks).
See the summary of functions and a list of examples and how-to pages in the MathWorks page on Debugging.
Related to the "db" functions is checkcode, which will check your code for possible problems before you even run it. This is a nice substitute for the red squiggly underlines that you would get in the MATLAB Editor.
Once you get a hang of dbstop and it's syntax, you won't often need to insert a keyboard into your code, but it's always an option.
Try placing the keyboard command in your code to insert a breakpoint. When the keyboard command is reached, MATLAB will drop into an interactive prompt that you can use to inspect variables. For example:
x = rand(10,10);
y = rand(10,5);
z = x * y;
keyboard; % you can interactively inspect x, y, z here
z = sort(z);
To leave keyboard mode, you can type dbquit to exit the program, or return to continue executing the program.
Another trick is to turn on dbstop if error which will automatically drop you into an interactive prompt whenever your code crashes.
You can use MATLAB -Dgdb if that helps. This sets gdb as the debugger. You will need to be familiar with gdb of course.
Once you do that, use the standard gdb commands to debug it.
EDIT
My mistake. Above won't work for M-Files. (Not having MATLAB to try things out is a pain :)
MATLAB has a pretty good set of debugging commands you can use from the commandline. If you insert keyboard commands in your MATLAB code, you can then use the commands.
You can use MATLAB's editor debug button to debug within MATLAB environment

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.