How can i generate .m file for a given simulink model?

1 view (last 30 days)
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?

Answers (3)

Angelo Yeo
Angelo Yeo on 23 Jul 2023
Unfortunately, there is no such a functionality to generate an equivalent m-file out of a Simulink model.

Sam Chak
Sam Chak on 23 Jul 2023
Edited: Sam Chak on 23 Jul 2023
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
  1 Comment
Aditi
Aditi on 25 Jul 2023
I do have a set of equations that describes the working of my motor.
Thank you for the link. I shall check it out.

Sign in to comment.


Steven Lord
Steven Lord on 25 Jul 2023
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.
  1 Comment
Aditi
Aditi on 26 Jul 2023
I need to generate a matlab function file.
Basically I want my matlab program to behave as a BLDC motor.

Sign in to comment.

Categories

Find more on Specialized Power Systems in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!