Run matlab m file from simulink

조회 수: 3 (최근 30일)
Nethaji Sasikala
Nethaji Sasikala 2017년 4월 9일
댓글: Walter Roberson 2025년 3월 26일
Hello, I have matlab M file(test.m), which have single line to call Python file [ !c:\-------\Python.py]. Kindly let me know, how to call this test.m from Simulink model.
I would like to run test.m, once the condition met in simulink model input.
Any reply will be greatly appreciated. Thanks

답변 (1개)

Riya
Riya 2025년 3월 26일
Hi Nethaji,
I understand that you are trying to execute a MATLAB script (test.m), which calls a Python file, from within a Simulink model when a specific condition is met based on Simulink inputs. There are two methods to do so:
Method 1: Using a MATLAB Function Block in Simulink
You can use a MATLAB Function Block to execute test.m when the condition is met. Inside the block, use the following function:
function call_script(u)
if u > threshold % Replace 'threshold' with your desired condition
evalin('base', 'test'); % Execute test.m
end
end
Kindly ensure that the “test.m” file is in MATLAB’s working directory or add its path using:
addpath('C:\path\to\your\script')
Finally, connect the block’s input to the signal that determines when to execute the script.
Method 2: Using an S-Function Block
You can also use an S-Function block with the following code:
function Outputs = call_script(~)
evalin('base', 'test');
end
This ensures that the “test.m” file runs only when triggered by Simulink’s input condition.
For further reference on executing MATLAB scripts from Simulink, kindly refer to the below official documentation:
  댓글 수: 1
Walter Roberson
Walter Roberson 2025년 3월 26일
Neither of these approaches are likely to work unless Rapid Acceleration is turned off.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by