Run matlab m file from simulink

3 次查看(过去 30 天)
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