How can i generate .m file for a given simulink model?
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (3)
Angelo Yeo
il 23 Lug 2023
Unfortunately, there is no such a functionality to generate an equivalent m-file out of a Simulink model.
Sam Chak
il 23 Lug 2023
Modificato: Sam Chak
il 23 Lug 2023
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
il 25 Lug 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.
Vedere anche
Categorie
Scopri di più su Specialized Power Systems in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!