How to call the function based on the simulink model?

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 Comments

The second and third calls to parameters have unbalanced parentheses. Assuming that's not really the issue you're interested in, what exactly are you trying to do and what errors are you getting?
Error using dodatkowe>parameters (line 42)
Error due to multiple causes.
Error in dodatkowe (line 7)
[wt1,wU1,wRd1]=parameters(Rd1,Ud1) - Show complete stack trace
Caused by:
Error using dodatkowe>parameters (line 42)
Error due to multiple causes. - Show complete stack trace
Error using dodatkowe>parameters (line 42)
Error evaluating parameter 'rep_seq_t' in 'zad3cw2_sim/Rd(t)' - Show complete stack trace
Error using dodatkowe>parameters (line 42)
Unrecognized function or variable 'wt'. - Show complete stack trace
Error using dodatkowe>parameters (line 42)
Variable 'wt' has been deleted from base workspace.
Suggested Actions:
• Load a file into base workspace. - Fix
• Create a new variable. - Fix
- Show complete stack trace
Error using dodatkowe>parameters (line 42)
Error evaluating parameter 'rep_seq_y' in 'zad3cw2_sim/Rd(t)' - Show complete stack trace
Error using dodatkowe>parameters (line 42)
Unrecognized function or variable 'wRd'. - Show complete stack trace
Error using dodatkowe>parameters (line 42)
Variable 'wRd' has been deleted from base workspace.
Suggested Actions:
• Load a file into base workspace. - Fix
• Create a new variable. - Fix
- Show complete stack trace
Error using dodatkowe>parameters (line 42)
Error due to multiple causes. - Show complete stack trace
Error using dodatkowe>parameters (line 42)
Error evaluating parameter 'rep_seq_t' in 'zad3cw2_sim/u(t)' - Show complete stack trace
Error using dodatkowe>parameters (line 42)
Unrecognized function or variable 'wt'. - Show complete stack trace
Error using dodatkowe>parameters (line 42)
Variable 'wt' has been deleted from base workspace.
Suggested Actions:
• Load a file into base workspace. - Fix
• Create a new variable. - Fix
- Show complete stack trace
Error using dodatkowe>parameters (line 42)
Error evaluating parameter 'rep_seq_y' in 'zad3cw2_sim/u(t)' - Show complete stack trace
Error using dodatkowe>parameters (line 42)
Unrecognized function or variable 'wU'. - Show complete stack trace
Error using dodatkowe>parameters (line 42)
Variable 'wU' has been deleted from base workspace.
Suggested Actions:
• Load a file into base workspace. - Fix
• Create a new variable. - Fix
- Show complete stack trace
>> 'dodatkowe'- is the name of matlab file
Earlier I have only Rd=0 and Ud=0 and basing on this I get 2 figures. My task is to display charts for other values of Rd and Ud. I want to use one code for this.
The starting point:
set(0,'defaultfigurecolor','w');
clear all
close all
U=5;
a=1;
b=1;
R=0.5;
L=0.2;
ep=0.1;
iz=U/R;
psz=L*iz;
epsi=iz*ep/100;
tp=-0.1;
tu=0;
T=L/R;
tk=round(tu+8*T);
dtmin=1e-2*T;
dtmax=2*dtmin;
Rd=0;
Ud=0;
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
What version of Simulink are you using?

Sign in to comment.

 Accepted Answer

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.

More Answers (2)

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 Comments

I have tried it, but I still get the same errors.
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.
i checked it, but in simulink I haven't implemented 'clear all' anywhere.

Sign in to comment.

Paul
Paul on 19 Oct 2021
Edited: Paul on 19 Oct 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.

Categories

Asked:

on 17 Oct 2021

Edited:

on 19 Oct 2021

Community Treasure Hunt

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

Start Hunting!