Code Error When Trying to Plot Scope to Matlab Graph
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Kassandra Krinke
le 14 Mar 2020
Commenté : Kassandra Krinke
le 18 Mar 2020
What would cause the code "plot(ScopeData.time,ScopeData.signals.values,'or')" to have an error and how to fix it. This is my code:
x0 = -1;
A = -2;
B = 1;
C = 1;
D = 0;
t = linspace(0,10);
u = 2 +3*sin(3*t);
[y,~]=lsim(A,B,C,D,u,t,x0);
plot(t,y);
hold on
plot(ScopeData.time,ScopeData.signals.values,'or');
xlabel('time(s)');
ylabel('x(t)');
legend('lsim','simulink','Location','northwest');
This is my Simulink
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/277166/image.jpeg)
4 commentaires
Aghamarsh Varanasi
le 17 Mar 2020
Modifié(e) : Aghamarsh Varanasi
le 17 Mar 2020
Hi Kassandra,
You are trying to plot ScopeData. ScopeData, which seems to be a structure, doesn't seem to be defined in the current scope. Could you please share your Simulink (.slx) file, pertaining to your model.
Réponse acceptée
Aghamarsh Varanasi
le 18 Mar 2020
Hi Kassandra,
It seems that you want to plot the Simulink Scope Data in MATLAB. Data Import/Export from Simulink can be set using the Configuration Parameters dialogue box. You could open the dialogue box from Simulation > Model Configuration Parameters, or press Ctrl+E. In the Data Import/Export Tab, you can select the variable in which you want to export the Simulink data. By default, the data is exported into a variable named ‘out’.
Hence your matlab code would be,
x0 = -1;
A = -2;
B = 1;
C = 1;
D = 0;
t = linspace(0,10);
u = 2 +3*sin(3*t);
[y,~]=lsim(A,B,C,D,u,t,x0);
plot(t,y);
hold on
xlabel('time(s)');
ylabel('x(t)');
legend('lsim','simulink','Location','northwest');
%The output of simulink is stored in a variable 'out'
%Access Scope data from out variable
time = out.ScopeData.time;
signals = out.ScopeData.signals.values;
plot(time, signals, '*g');
Make sure that you simulate the Simulink model before running the MATLAB Code.
Hope this helps.
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!