How to use a slider

9 visualizaciones (últimos 30 días)
Juan Pedro Martinez
Juan Pedro Martinez el 13 de Sept. de 2022
Comentada: Juan Pedro Martinez el 29 de Sept. de 2022
Hello, I am preparing a class on phasors. I want to make a symple plot for an RC circuit, in which I want to include a slider to vary frecuency and change the phasors. Does anyone have any pointers? I um struggling with the uicontrols. This is one plot, but I have not been able to make the slider work (I excluded here the things I tried, because I'm pretty lost). The slider should update w, I have tried making the plot as a function of a), but do not know how to update the parameter from the slider value.
Thank you
close all
clear
clc
%----
R=1000;%ohm
C=100*10^(-9);%nf
V0=1;
%---------
fc=1/(R*C*2*pi);
%w=@(a)a;
w=2*pi*fc;
%----
fig=figure;
I0=V0./(R-(1i/(w*C)));
VR=I0*R;
VC=-(1i./(C.*w))*I0;
c=compass([V0,VR,VC]);
c1=c(1);
c1.LineWidth=2;
c2=c(2);
c2.LineWidth=2;
c2.Color='r';
c3=c(3);
c3.LineWidth=2;
c3.Color='g';
legend('V0','VR','VC');
set(gca,'FontSize',18)
%-----Slider---------
b = uicontrol('Parent',fig,'Style','slider','Position',[81,54,419,23],...
'value',w, 'min',0.01*2*pi*fc, 'max',15*2*pi*fc);
bgcolor = c.Color;
bl1 = uicontrol('Parent',fig,'Style','text','Position',[50,54,50,50],...
'String','0.01fc','BackgroundColor',bgcolor);
bl2 = uicontrol('Parent',fig,'Style','text','Position',[500,54,50,50],...
'String','15fc','BackgroundColor',bgcolor);
bl3 = uicontrol('Parent',fig,'Style','text','Position',[240,25,100,23],...
'String','Frecuencia','BackgroundColor',bgcolor);

Respuesta aceptada

Cris LaPierre
Cris LaPierre el 13 de Sept. de 2022
Editada: Cris LaPierre el 13 de Sept. de 2022
My preference would be to create app (simple tutorial incorporates a slider to adjust a plot), or perhaps use a live script with a slider control added to set the value of w.
However, pursuing your approach is valid as well. You just need to incorporate a callback function into your slider (see this example). The callback function exectues everytime the slider is moved. Here's a quick (but probably not ideal) example using your code.
%----
R=1000;%ohm
C=100*10^(-9);%nf
V0=1;
%---------
fc=1/(R*C*2*pi);
%w=@(a)a;
w=2*pi*fc;
%----
fig=figure;
I0=V0./(R-(1i/(w*C)));
VR=I0*R;
VC=-(1i./(C.*w))*I0;
c=compass([V0,VR,VC]);
c1=c(1);
c1.LineWidth=2;
c2=c(2);
c2.LineWidth=2;
c2.Color='r';
c3=c(3);
c3.LineWidth=2;
c3.Color='g';
legend('V0','VR','VC');
set(gca,'FontSize',18)
%-----Slider---------
b = uicontrol('Parent',fig,'Style','slider','Position',[81,54,419,23],...
'value',w, 'min',0.01*2*pi*fc, 'max',15*2*pi*fc);
b.Callback = @myFcn;
bgcolor = c.Color;
bl1 = uicontrol('Parent',fig,'Style','text','Position',[50,54,50,50],...
'String','0.01fc','BackgroundColor',bgcolor);
bl2 = uicontrol('Parent',fig,'Style','text','Position',[500,54,50,50],...
'String','15fc','BackgroundColor',bgcolor);
bl3 = uicontrol('Parent',fig,'Style','text','Position',[240,25,100,23],...
'String','Frecuencia','BackgroundColor',bgcolor);
function myFcn(src,event)
w = src.Value;
R=1000;%ohm
C=100*10^(-9);%nf
V0=1;
I0=V0./(R-(1i/(w*C)));
VR=I0*R;
VC=-(1i./(C.*w))*I0;
c=compass(gca,[V0,VR,VC]);
c(1).LineWidth=2;
c(2).LineWidth=2;
c(2).Color='r';
c(3).LineWidth=2;
c(3).Color='g';
end
  1 comentario
Juan Pedro Martinez
Juan Pedro Martinez el 29 de Sept. de 2022
Hello Cris, thank you for your answer, it was extremely helpful, and it worked. The students understood it better I think, and I got a better idea on how to use callback functions.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by