Can the MATLAB Engine API implement interactive operations in the MATLAB Command Window?

조회 수: 2 (최근 30일)
In the MATLAB interface window, when running some programs or scripts, some interactive instructions are required, such as giving a prompt in the command window and asking the user to enter some numbers ,Y or N, and pressing the enter key, then the program will continue to execute based on the input content. Can the above operation be achieved using Python through the Matlab Engine API interface without opening the Matlab interface now?The following is a function that requires interactive operations in the command window of Matlab:
function my_function()
disp('Step 1: Please enter a number:');
x = input('');
disp(['You entered: ', num2str(x)]);
disp('Step 2: Please enter another number:');
y = input('');
disp(['You entered: ', num2str(y)]);
end
How to use Python to call the above function through the Matlab Engine API and achieve its normal operation? The prompt words and input content are completed through programming or input in Python.

답변 (1개)

SACHIN KHANDELWAL
SACHIN KHANDELWAL 2024년 6월 25일
Hi @HCH ,
You can use the MATLAB Engine API to interact with MATLAB from Python. However, if you need to perform interactive operations in the MATLAB command window, it can be tricky.
Instead, I recommend using MATLAB's built-in functions directly in your Python code to streamline your workflow. For more details, check out the MathWorks documentation on the MATLAB Engine API for Python.
You might also find the following getting started page helpful for your workflow:
Hope it helps!
  댓글 수: 1
HCH
HCH 2024년 6월 30일
Hello SACHIN KHANDELWAL, thank you for your thoughtful answer!
We can continue to discuss it, or help me analyze it. I ran a Matlab script file in Python through the Matlab Engine API and found that prompt words that should have been displayed in the Matlab command window can be displayed on the Python terminal. The prompt words here refer to the information that is usually displayed in the input() function to help users in the Matlab command window. Does that mean that Python can perform interactive operations on Matlab script files through the Matlab Engine API?
Another question I would like to ask is: when using Matlab Engine for Python, most of the time Matlab is not started, which means the command window is not displayed. In this case, the input() function can still work because the input() function plays a crucial role in interactive operations. Currently, the system does not enable the command window by default. Will the input() function also be restricted or can it be completed in the background?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Call MATLAB from Python에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by