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

 Réponse acceptée

Rik
Rik le 9 Juil 2019
Modifié(e) : Rik le 9 Juil 2019

1 vote

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 commentaires

Pavan Kumar
Pavan Kumar le 9 Juil 2019
Can you tell me how to proceed with this
Rik
Rik le 9 Juil 2019
See my edited answer for a code example.
Pavan Kumar
Pavan Kumar le 9 Juil 2019
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?
Rik
Rik le 9 Juil 2019
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.
Pavan Kumar
Pavan Kumar le 10 Juil 2019
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.
Rik
Rik le 10 Juil 2019
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
Pavan Kumar
Pavan Kumar le 10 Juil 2019
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 le 10 Juil 2019
Modifié(e) : Pavan Kumar le 10 Juil 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?
Rik
Rik le 10 Juil 2019
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.
Pavan Kumar
Pavan Kumar le 10 Juil 2019
ANd also have some indications on the slider
Rik
Rik le 10 Juil 2019
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).
Pavan Kumar
Pavan Kumar le 10 Juil 2019
ok..thanks a lot bro..u dont know wha thelp u did to me...
Pavan Kumar
Pavan Kumar le 11 Juil 2019
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.
Rik
Rik le 11 Juil 2019
What is the change? I've show you the way the edit them, what did you try yourself?

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by