Command window execution by script editor automatically in a loop

9 次查看(过去 30 天)
Good morning,
I want to execute system commands through matlab and to run system command in a loop. System command is working fine for one iteration. Actually the problem is i have to run system command in a loop where system command runs multiple times until meets user criterio and then stop. So my question is when system command executed in second iteration in a loop user will have to give certain input to continue the process like below for loop to continue until it stops (not meeting criterio)
THese are the steps happen in command window after second iteration starts through system command in matlab.
Enter "exit" to end with restart save, "quit" to end without restart save. quit then press enter
Continue with interactive input options for control? <y or n> : y then press enter
Specify command (INTEractive, INCLude, STOP, etc.) STOP then press enter
So my question is . Is there a way to execute this commands automatically in command window without user interventi?
  1 个评论
Mario Malic
Mario Malic 2021-1-27
You can, but it's hard (I don't think that there's posted solution in here, but only suggestions how to do it). Use search tool to look for information. Look for replies/comments from Walter Roberson.

请先登录,再进行评论。

回答(1 个)

Sanchari
Sanchari 2024-5-23
Hello Rakesh,
MATLAB's native system command doesn't directly support interactive inputs.
One common workaround approach is to prepare a script file that contains all the commands to be executed, including responses to prompts, and then execute this script file using the system command from within MATLAB. The feasibility of this approach depends on the external application's ability to accept input from a file or to be controlled through input redirection.
Example 1: Using a Script File
If the external application can read commands from a file, one can create a text file with all the necessary commands and inputs:
quit
y
STOP
Then, one can call the external application and have it read this file as its input source. The command to do this will depend on the application. For example, if the application supports reading commands from a file using the “<” redirection operator, one could do something like:
system('your_command < commands.txt');
Example 2: Using Input Redirection
For applications that do not directly support reading commands from a file, one might be able to use input redirection (if supported by the operating system's shell) to simulate interactive inputs:
commandStr = 'echo quit | echo y | echo STOP | your_command';
[status, cmdout] = system(commandStr);
This approach constructs a single command string that echoes the desired inputs and pipes them into the required command. Note that the specifics of how to chain these commands can vary between shells and might require adjustment to work correctly.
Example 3: Using a Batch File or Shell Script
Another approach is to write a batch file (Windows) or a shell script (Unix/Linux) that runs the required command and automatically handles the inputs. This script would then be executed from MATLAB:
For Windows (batch file):
@echo off
echo quit
echo y
echo STOP
| your_command
Save this as “run_command.bat” and execute it from MATLAB:
system('run_command.bat');
For Unix/Linux (shell script):
#!/bin/bash
echo quit
echo y
echo STOP
| your_command
Save this as “run_command.sh”, make it executable (“chmod +x run_command.sh”), and execute it from MATLAB:
system('./run_command.sh');
The exact implementation will depend on the external command's behavior and the operating system. Some applications might not support any form of input redirection or might have specific command-line options to enable non-interactive modes.
Please refer to the following references to know further about:
  1. Continue entering statement in command window (ML Answer): https://www.mathworks.com/matlabcentral/answers/123339-continue-entering-statement-in-command-window-no-o-p?s_tid=answers_rc1-3_p3_Topic
  2. How to run loop until user enters text in the command window (ML Answer): https://www.mathworks.com/matlabcentral/answers/2032529-how-to-run-loop-until-user-enters-text-in-the-command-window-without-using-the-input-function?s_tid=answers_rc1-1_p1_Topic
  3. Copy MATLAB command window without diary (ML Answer): https://www.mathworks.com/matlabcentral/answers/545957-copy-matlab-command-window-without-diary?#answer_568048
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by