How to use a slider

1 view (last 30 days)
Juan Pedro Martinez
Juan Pedro Martinez on 13 Sep 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);

Accepted Answer

Cris LaPierre
Cris LaPierre on 13 Sep 2022
Edited: Cris LaPierre on 13 Sep 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 Comment
Juan Pedro Martinez
Juan Pedro Martinez on 29 Sep 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.

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by