How can i generate .m file for a given simulink model?
조회 수: 1 (최근 30일)
이전 댓글 표시
So i have basically modeled a BLDC motor in simulink for my project. But now i need to model the same in matlab. So is there a way in which i can directly generate the .m file for my Simulink model?
댓글 수: 0
답변 (3개)
Angelo Yeo
2023년 7월 23일
Unfortunately, there is no such a functionality to generate an equivalent m-file out of a Simulink model.
Sam Chak
2023년 7월 23일
편집: Sam Chak
2023년 7월 23일
Hi @Aditi
What kind of code do you expect to see in the m-file? If you want to extract some information related to the system that you modeled in Simulink, in your case, the BLDC motor, then it may be possible to obtain a linear approximation (state-space model) of the Simulink model using the linearize() command. For more info, please refer to the documentation:
From the state-space model, you can write a simple script to simulate the linearized model over a small operating range, either using step(), lsim(), impulse(), or even an ode solver such as ode45().
If you know the exact math that describes the behavior of the motor, then you can write the ode function file for the motor. See the example below:

function xdot = pmstepper(t, x)
xdot = zeros(4, 1);
% parameters
B = ...;
Kb = ...;
I = ...;
Rt = ...;
R = ...;
L = ...;
K1 = B/I;
K2 = Kb/I;
K3 = Rt;
K4 = Kb/L;
K5 = R/L;
va = ...;
vb = ...;
% state-space
xdot(1) = x(2);
xdot(2) = - K1*x(2) - K2*x(3)*sin(K1*x(3)) + K2*x(4)*cos(K1*x(3));
xdot(3) = K4*x(2)*sin(K1*x(3)) - K5*x(3) + 1/L*va;
xdot(4) = - K4*x(2)*cos(K1*x(3)) - K5*x(4) + 1/L*vb;
end
Steven Lord
2023년 7월 25일
Do you need to generate a MATLAB function file or do you just need to be able to simulate the model from MATLAB code? If the latter you can do that.
참고 항목
카테고리
Help Center 및 File Exchange에서 Specialized Power Systems에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!