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 Center および File ExchangeCall Python from MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by