Run matlab m file from simulink
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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
0 comentarios
Respuestas (1)
Riya
el 26 de Mzo. de 2025
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 comentario
Walter Roberson
el 26 de Mzo. de 2025
Neither of these approaches are likely to work unless Rapid Acceleration is turned off.
Ver también
Categorías
Más información sobre Call Python from MATLAB en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!