Slider which changes images

Dear Friends,
COuld you suggest me how to do this.
I have a program in matlab where in , the XY graph is a function of applied voltage.
When i change the votlage, the shape of the graph also changes in the way as shown in the three pics.
I want to create a slider (where the slider drag represents the voltage) and want the XY graph to change as the slider is moved.
But i dont know how to do it. Can anyone suggest me a way to deal with this.
Thanking you.
1.png
2.png
3.png

 Accepted Answer

Rik
Rik on 9 Jul 2019
Edited: Rik on 9 Jul 2019
Live updates are a bit more tricky, but you can use the uicontrol function to create a slider. Then you can use the callback function to change the YData according to your selected voltage. (changing the YData property of you line object is much faster than clearing the axes and calling plot again).
Let me know if you need a small example.
Edit:
The example below shows how you could create a GUI that helps you find out the effect of changing a particular value in polyval. This examples works with continuous data, but your situation might not. You can use round on the Value property to ensure interger outputs.
h=struct;%prepare guidata struct
h.f=figure(999);clf(h.f)%open a clean figure
h.ax=axes('Parent',h.f,...
'Units','Normalized',...
'Position',[0.1 0.2 0.8 0.75]);
%prepare data
h.x=linspace(0,1,1000);
h.p=[6.5 -15 NaN -3 0];
%initialize plot
p=h.p;p(3)=0;
y=polyval(p,h.x);
h.line=plot(h.x,y,'Parent',h.ax);
%create slider
h.slider=uicontrol('Parent',h.f,...
'Units','Normalized',...
'Position',[0.1 0.05 0.8 0.09],...
'Style','slider',...
'Min',5,'Max',15,'Value',10,...
'Callback',@slider_callback);
h.legend=legend(h.line,sprintf('V=%.1f',p(3)));
%store handles and data to guidata
guidata(h.f,h)
function slider_callback(hObject,eventdata) %#ok<INUSD>
h=guidata(hObject);%retrieve struct
p=h.p;p(3)=get(hObject,'Value');%set slider value to p(3)
y=polyval(p,h.x);
set(h.line,'YData',y)%update plot
set(h.legend,'String',{sprintf('V=%.1f',p(3))})%update legend
end

14 Comments

Can you tell me how to proceed with this
See my edited answer for a code example.
Dear Rik,
I actually have a program by which the voltage dependent plot is generated...is there any way i can insert the program code in the code u sent me?
Sure you can. You can see how my code works. It shouldn't be too hard to put my code around your exisiting function. If you don't share any other details about your code, I cannot provide a more meaningful answer.
The most ideal situation would be if you can chop up you current function in two parts: one part that finds the intensity given a voltage, and another part that generates the rest of the plot based on that data. The you can put a call to the intensity function in the callback to retrieve the new y-values of your plot.
Asure...i can share with the code.....Here they are...
it consists of Part1 and Part2...Part1 is a calling function in Part2.....
The Intensity and the voltage dependent code are defined in Part2.
I have attached the two parts here.
Actually, am not into GUI..so not experienced in this type of coding..But however, i felt, if i can
keep it for a presentation, it would make things more clearer.
Coudl you help me out.
The problem you have is converting your code to a function so it only depend on the voltage and some parameters. In de code below I have converted that script and function to two functions. One function gets the parameters that don't depend on the voltage, which decreases the calculation time for a voltage change. The other function takes those parameters and calulates the y-values.
Also, I don't know what kind of resolution you're working at that you want the font to be so enormous.
h=struct;%prepare guidata struct
h.f=figure(999);clf(h.f)%open a clean figure
h.ax=axes('Parent',h.f,...
'Units','Normalized',...
'Position',[0.1 0.2 0.8 0.75]);
%prepare data
h.params=get_parameters;
h.x=h.params.Ls;
h.legend_FormatSpec='V=%.0f';
%initialize plot
V=4500;
y=get_intensity(V,h.params);
h.line=plot(h.x,y,'Parent',h.ax,'LineWidth',4);
%set other axis graphics
xlabel('Wavelength(\mum)','FontSize',30,'FontWeight','bold');
ylabel('Intensity (Arb. Units)','FontSize',30,'FontWeight','bold');
set(h.ax,'fontsize',30,'fontweight','bold');
ylim(h.ax,[0 1.2]);
xlim(h.ax,[1.15 3.2500]);
grid(h.ax,'on')
%create slider, using the V value used to initialise the plot
h.slider=uicontrol('Parent',h.f,...
'Units','Normalized',...
...'Position',[0.1 0.025 0.8 0.05],... horizontal slider
'Position',[0.95 0.2 0.025 0.75],... vertical slider
'Style','slider',...
'Min',-6000,'Max',6000,'Value',V,...
'BackgroundColor',[1 1 1],...
'Callback',@slider_callback);
h.legend=legend(h.line,sprintf(h.legend_FormatSpec,V));
%store handles and data to guidata
guidata(h.f,h)
function slider_callback(hObject,eventdata) %#ok<INUSD>
h=guidata(hObject);%retrieve struct
V=get(hObject,'Value');
y=get_intensity(V,h.params);
set(h.line,'YData',y)%update plot
set(h.legend,'String',{sprintf(h.legend_FormatSpec,V)})%update legend
end
function intensity=get_intensity(Vol,params)
%----APPLIED VOLTAGE -6000 V to 6000 V---%
[Lcen,Lp,Ls,Li]=deal(params.Lcen,params.Lp,params.Ls,params.Li);
%original calculations left as reference
%delni = -(0.5*r33.*Vol.*(ne_i).^3)/t;
delni=Vol*params.delni_factor;
%delns = -(0.5*r33.*Vol.*(ne_s).^3)/t;
delns=Vol*params.delns_factor;
%delnp = -(0.5*r33.*Vol.*(ne_p).^3)/t;
delnp=Vol*params.delnp_factor;
delphi = (2*pi*(Lcen)).*((delnp./Lp) - (delns./Ls) - (delni./Li));
intensity = params.intensity_1.*(cos((delphi./2)+params.intensity_2)).^2;
intensity=intensity/max(intensity);
end
function params=get_parameters
tau = 26.78;
T = 23;
m = 1;
Lp = 0.929;
Ls = (1.10:0.001:4.500);
Li = (Lp.*Ls)./(Ls-Lp);
L13 = 5000;
Lcen = 10000;
ne_p = Part1(Lp,T);
ne_s = Part1(Ls,T);
ne_i = Part1(Li,T);
kp = (2*pi.*ne_p)./Lp;
ks = (2*pi.*ne_s)./Ls;
ki = (2*pi.*ne_i)./Li;
Kg = (2*pi*m)/tau;
delk = kp - ki - ks - Kg;
t = 500;
r33 = 34e-6 ;
params=struct;%prepare a struct to hold the parameters
params.delni_factor= -(0.5*r33.*(ne_i).^3)/t;
params.delns_factor= -(0.5*r33.*(ne_i).^3)/t;
params.delnp_factor= -(0.5*r33.*(ne_s).^3)/t;
params.intensity_1=(((sin(0.5.*delk*(L13)))./(0.5.*delk*(L13))).^2);
params.intensity_2=(2*(0.5.*delk*(L13)));
[params.Lcen,params.Lp,params.Ls,params.Li]=deal(Lcen,Lp,Ls,Li);
end
function ne = Part1(w,T)
a1 = 5.35583;
a2 = 0.100473;
a3 = 0.20692;
a4 = 100;
a5 = 11.34927;
a6 = 1.5334*10^-2;
b1 = 4.629*10^-7;
b2 = 3.862*10^-8;
b3 = -0.89*10^-8;
b4 = 2.657*10^-5;
F = (T - 24.5).*(T + 570.82);
n1 = a1 + (b1*F);
n2 = (a2+(b2*F))./((w).^2-(a3+(b3*F)).^2);
n3 = (a4+(b4*F))./((w).^2-a5.^2);
n4 = a6.*(w).^2;
ne=(sqrt(n1+n2+n3-n4));
end
Dear Rik,
Thanks a lot for the code.
I somehow find that the intensity plot generated by your code for a specific value is different from what my code generates.
For Eg....For a Vol value of 2040 volts, my script generates the waveform as attached.
and when i scroll the slider to 2040, the waveform is different.
Mine_2040Volts.png
Yourscript_2040Volts.png
Pavan Kumar
Pavan Kumar on 10 Jul 2019
Edited: Pavan Kumar on 10 Jul 2019
I found the error bro.....Line 74, 75, 76 wrongly defined...now it looks perfect.....can i change the slider to vary more slowly?
Ah yes, sorry about that, that's the down side of copy paste code.
Anyway, happy to help. If this answer solved your issue, please consider marking it as accepted answer. If not, feel free to comment with your remaining issues.
ANd also have some indications on the slider
Your code specifies the limits of V are -6000 to 6000, so I made that the min and max. If you want to change that, you can change those properties of the slider.
Indicators on the slider are a bit more tricky. You could put an axis under the slider and set YTicks. If you put the axis there before you put in the slider it should automatically hide the axis itself. Note that using a second axis in the figure may interfere with any calls to gca, which is one of the reasons you should always use explicit target axes (as I already did in this code).
ok..thanks a lot bro..u dont know wha thelp u did to me...
Hi bro..there is a small change in the program code line..could you help me out in makin the program..attaching the two function codes part1 and part 2 again.
What is the change? I've show you the way the edit them, what did you try yourself?

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Asked:

on 9 Jul 2019

Commented:

Rik
on 11 Jul 2019

Community Treasure Hunt

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

Start Hunting!