Slider which changes images

16 次查看(过去 30 天)
Pavan Kumar
Pavan Kumar 2019-7-9
评论: Rik 2019-7-11
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

采纳的回答

Rik
Rik 2019-7-9
编辑:Rik 2019-7-9
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 个评论
Pavan Kumar
Pavan Kumar 2019-7-11
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 2019-7-11
What is the change? I've show you the way the edit them, what did you try yourself?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by