Taskill in matlab by PID - matlab

I have the following matlab code to close a cmd window
pid = 21324
if ispc
cmd = sprintf('taskkill /PID %d', pid);
else
cmd = sprintf('kill %d', pid);
end
system(cmd)
But I get the following error
Error using TaskKill Too many input arguments.
MATLAB:TooManyInputs Error: Too many input arguments.
I tried to find examples and documentation but it is supposed to work
I am using matlab R2018b and windows

Related

Execute matlab script from ubuntu terminal but getting error "matlab: command not found"

I have installed matlab 2018a I want to execute matlab script from ubuntu(18.04 LTS) terminal. Matlab installation path is "/usr/local/MATLAB/R2018a". Now I run : matlab -nodisplay -nosplash -nodesktop -r "run('/home/siddhartha/Documents/matlab_code/test_matlab_script.m');exit;" to execute test_matlab_script.m script.
The script contain :
clear all;
a = 1;
b = 1;
c = a+b;
disp(c);
but I am getting error matlab: command not found
Please help me in how to overcome this error and successfully execute the matlab script from terminal

matlab R2020a Problem creating and writing to file when running in background on Linux

I am working on linux with matlabR2020a. My program test.m is below:
%test.m
fid=fopen(strcat('test','.txt'),'w');
fprintf(fid, '%.4f %.4f\n', 0.1, 0.2);
fclose(fid);
exit;
When I run the program like this it works fine:
matlab -nodisplay -nodesktop -r "test"
However, when I try to run in the background, it fails to create the file:
matlab -nodisplay -nodesktop -r "test" &
Any help is greatly appreciated.

How to send commands to command window programmatically in MATLAB?

In matlab I can change to another shell by the bang (!) Notation.
Example:
I enter a conda Environment in MATLAB by the following command:
!cmd '"%windir%\System32\cmd.exe" /K ""C:\Program Files\Anaconda3\Scripts\activate_<conda-env-name>.bat" "C:\Program Files\Anaconda3""'
My MATLAB Command window then Displays following:
(<conda-env-name>) U:\some_starting_path>
Now, is there a way to send commands to this newly entered shell in a programmatic way, so that that command is evaluated in that very shell's syntax and not as a MATLAB-command?
For example, how can I write code that will execute a Python command without needing to enter it manually into the command line?
Not using the ! command or system(). Those are "one and done" functions.
But you can use Java's java.lang.Process API from within Matlab to control and interact with an ongoing process.
function control_another_process
pb = java.lang.ProcessBuilder(["myCommand", "myArg1", "myArg2"]);
proc = pb.start; % now proc is a java.lang.Process object
stdin = proc.getOutputStream; % Here's how you send commands to the process
stdout = proc.getInputStream; % And here's how you get its output
stderr = proc.getErrorStream;
% ... now do stuff with the process ...
end
You can use this with a shell, with python, or any other command.
Here's a Matlab class that wraps up the Java code to make it convenient to work with in Matlab: https://github.com/apjanke/janklab/blob/master/Mcode/classes/%2Bjl/%2Butil/Process.m

Matlab exit force with return code greater than zero

I've a Matlab R2007b script which I call from a batch script like this:
matlab.exe -nosplash -nodesktop -r my_script
I've a try/catch block in the Matlab script and on error it runs exit(1), to indicate to the calling process (indirectly, Jenkins!) that the process has failed.
This works great except occasionally on exit via error Matlab wants to save any modified files and pops up a save dialogue (not great when you're running headless with Jenkins!). I can avoided this with a 'exit force' but then my return code is always 0 (indicating success to Jenkins).
Is it possible in Matlab to force exit AND set a return code greater than zero?
exit(1) force
I have also tried to figure out a solution to this issue. Since working with environment variables did not work out the way we wanted, I created a workaround with a dummy file that is created whenever the Matlab call fails and then checked in Jenkins (currently no Matlab access, maybe some quotes are wrong):
matlab -r "try, [returnVal]=ScriptName;catch, disp('exception occurred']); returnVal= -1; end, if (returnVal~= 0), fid = fopen('errorFile.txt','wt'); fclose(fid); end, exit force"
IF EXIST errorFile.txt (exit /b 1)
Generally, of course, it also makes sense to include additional arguments in the matlab call, for Windows, e.g.:
matlab -log -nosplash -nodesktop -noFigureWindows -minimize -wait

How can I stop MATLAB from returning until after a command-line script completes?

I see in the MATLAB help (matlab -h) that I can use the -r flag to specify an m-file to run. I notice when I do this, MATLAB seems to start the script, but immediately return. The script processes fine, but the main app has already returned.
Is there any way to get MATLAB to only return once the command is finished? If you're calling it from a separate program it seems like it's easier to wait on the process than to use a file or sockets to confirm completion.
To illustrate, here's a sample function waitHello.m:
function waitHello
disp('Waiting...');
pause(3); %pauses 3 seconds
disp('Hello World');
quit;
And I try to run this using:
matlab -nosplash -nodesktop -r waitHello
Quick answer:
matlab -wait -nosplash -nodesktop -r waitHello
In Matlab 7.1 (the version I have) there is an undocumented command line option -wait in matlab.bat. If it doesn't work for your version, you could probably add it in. Here's what I found. The command at the bottom that finally launches matlab is (line 153):
start "MATLAB" %START_WAIT% "%MATLAB_BIN_DIR%\%MATLAB_ARCH%\matlab" %MATLAB_ARGS%
The relevant syntax of the start command (see "help start" in cmd.exe) in this case is:
start ["window title"] [/wait] myprogram.exe args ...
A bit higher, among all of the documented command line options, I found (line 60):
) else if (%opt%) == (-wait) (
set START_WAIT=/wait
) else (
So specifying -wait should do what you want, as long as you're also exiting matlab from your script (otherwise it will wait for you to terminate it interactively).