Filter löschen
Filter löschen

How to call the function based on the simulink model?

1 Ansicht (letzte 30 Tage)
Aleksandra Pawlak
Aleksandra Pawlak am 17 Okt. 2021
Bearbeitet: Paul am 19 Okt. 2021
Hi there!
I have problem with my code - I get error due to multiple causes.
I need three figures- depending on the different values of the Rd and Ud. I want to place my code in the function conected with the simulink model.
What should I modify?
clc
close all
clear all
Rd1=0;
Ud1=0;
[wt1,wU1,wRd1]=parameters(Rd1,Ud1)
Rd2=5*R;
Ud2=0;
[wt2,wU2,wRd2]=parameters((Rd2,Ud2)
Rd3=0;
Ud3=-0.5*U;
[wt3,wU3,wRd3]=parameters((Rd3,Ud3)
function [wt,wU,wRd]=parameters(Rd,Ud)
U=5;
a=1;
b=1;
R=0.5;
L=0.2;
ep=0.1;
tp=-0.1;
tu=0;
T=L/R;
tk=round(tu+8*T);
tpw=tk;
tk=10*tk;
wt=[tp tu tu+10e-12 tpw tpw+10e-12 tk];
wU=[0 0 U U Ud Ud];
wRd=[0 0 0 0 Rd Rd];
sim('zad3cw2_sim');
t=ans.ws(:,1);
In=ans.ws(:,2);
Il=ans.ws(:,3);
Psn=ans.ws(:,4);
Psl=ans.ws(:,5);
En=ans.ws(:,6);
El=ans.ws(:,7);
sn=ans.wsl(:,1);
sl=ans.wsl(:,2);
tzn=t(end)-tpw;
tzl=t(find(sl>0,1,'first'))-tpw;
figure('name','Wyniki symulacji w funkcji czasu','numberTitle','off');
h1=plot(t,Il,'b',t,In,'r');
hold on
h2=plot(t,Psl,'b',t,Psn,'r','Linewidth',1.5);
h3=plot(t,El,'--b',t,En,'--r','Linewidth',1.5);
hold off
legend([h1;h2;h3],'Prąd u.l.','Prąd u.nl.','Strumień u.l.','Strumień u.nl.','Energia u.l.','Energia u.nl.');
xlabel('Czas, s')
ylabel('Prąd, A, Strumień, Wb, Energie, J');
grid
figure('name','Przebiegi prądów wyznaczanie czasów zaniku','numberTitle','off');
h1=plot(t,Il,'b',t,In,'r');
hold on
h2=plot(t,sl,':b',t,sn,':r');
hold off
legend([h1;h2],'Prąd u.l.','Prąd u.nl.','Koniec zaniku u.l.','Koniec zaniku u.nl.');
text(tzn+tpw,0.1*iz,['tzn = ' num2str(tzn,'%0.3f') ' s'],'HorizontalAlignment','left','BackgroundColor',[1 1 1]);
text(tzl+tpw,0.2*iz,['tzl = ' num2str(tzl,'%0.3f') ' s'],'HorizontalAlignment','left','BackgroundColor',[1 1 1]);
xlabel('Czas, s')
ylabel('Prąd, A, ');
grid
end
  5 Kommentare
Paul
Paul am 19 Okt. 2021
What version of Simulink are you using?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Sara Nadeau
Sara Nadeau am 18 Okt. 2021
It's a little hard to say without knowing the specific errors you're getting. However - is there a reason you aren't assigning the output of the sim function to a variable? Have you tried updating the call to sim to look like:
out = sim('zad3cw2_sim');
Then, you'd update all your statements that access the output data to use the out variable (or whatever you choose to name it) instead of ans.

Weitere Antworten (2)

Walter Roberson
Walter Roberson am 17 Okt. 2021
What should I modify?
You should get rid of the "clear all": it is erasing variables in the base or function workspace that your code is expecting to exist.
You should probably get rid of the "close all" as well, as that might affect scopes or objects drawn by Simulink.
Getting rid of the "clc" would probably be a good idea too.
  4 Kommentare
Walter Roberson
Walter Roberson am 19 Okt. 2021
Bearbeitet: Walter Roberson am 19 Okt. 2021
Make sure that you do not have any MATLAB Function Blocks that use "clear all" or "assignin('base')" and that none of your simulink object initialization function invoke clear all either.
Aleksandra Pawlak
Aleksandra Pawlak am 19 Okt. 2021
i checked it, but in simulink I haven't implemented 'clear all' anywhere.

Melden Sie sich an, um zu kommentieren.


Paul
Paul am 19 Okt. 2021
Bearbeitet: Paul am 19 Okt. 2021
Since you're using 2020b, try changing your sim() command to:
out = sim('zad3cw2_sim','SrcWorkspace','current')
This should probably work, but there are some caveats, so I suggest you check the doc page for sim() to see if they apply to your case.
However, SrcWorkspace is not supported for versions later than 2020b. So if you want a solution that works in 2020b and later versions, you can call sim() with a Simulink.SimulationInput object. Take a look at that doc page and then feel free to reply here if you have any questions on how to make that work.
There are other options should neither of these meet your needs. In fact, this question has come up here several times and some searching should quickly find relevant discussions that cover these other options.

Kategorien

Mehr zu Programmatic Model Editing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by