How to use slider in matlab for varying two values in graph?
4 views (last 30 days)
Show older comments
HELLO,
I have reading of freq vs ampl for different s and h.
as slider moves over different s and h the exact graph should be displayed in the window.
How to create it SLIDER IN A PLOT?
here I have attached the example of excel data that i have.
Please help me to get in figure!
Semilogx(freq,amp) THE FUNCTION THAT I USE TO PLOT THE GRAPH
0 Comments
Accepted Answer
Rik
on 9 Mar 2020
Edited: Rik
on 9 Mar 2020
You should create the plot and use the slider callback function to modify the underlying data.
The code below should get you most of the way there.
freq=[1;3;5;9;11;13;15;18;20;23;25;28;30;32;35;37;40;42;45;47;49;52;54;57;59;62;64;66;69;71;74;76;79];
s=2;h=2;%intial
data=cell(4,5);
data{2,2}=[10;20;30;40;50;60;70;80;90;100;110;120;130;140;150;160;170;180;190;200;210;220;230;240;250;260;270;280;290;300;310;320;330];
data{2,4}=[3;6;9;12;15;18;21;24;27;30;33;36;39;42;45;48;51;54;57;60;63;66;69;72;75;78;81;84;87;90;93;96;99];
data{2,5}=[15;30;45;60;75;90;105;120;135;150;165;180;195;210;225;240;255;270;285;300;315;330;345;360;375;390;405;420;435;450;465;480;495];
data{4,2}=[20;40;60;80;100;120;140;160;180;200;220;240;260;280;300;320;340;360;380;400;420;440;460;480;500;520;540;560;580;600;620;640;660];
data{4,4}=[50;100;150;200;250;300;350;400;450;500;550;600;650;700;750;800;850;900;950;1000;1050;1100;1150;1200;1250;1300;1350;1400;1450;1500;1550;1600;1650];
data{4,5}=[2;4;6;8;10;12;14;16;18;20;22;24;26;28;30;32;34;36;38;40;42;44;46;48;50;52;54;56;58;60;62;64;66];
handles=struct;
handles.f=figure(1);clf(handles.f)
axes(handles.f,'Units','Normalized','Position',[0.1 0.3 0.85 0.65])
handles.s=2;handles.h=2;handles.data=data;
handles.plot=semilogx(freq,data{s,h});
handles.slider_s=uicontrol(handles.f,'Style','slider',...
'Units','Normalized','Position',[0.1 0.05 0.85 0.08],...
'Min',1,'Max',size(data,1),'Value',s,...
'Callback',@(obj,event) slidercallback(obj,'s'));
handles.slider_h=uicontrol(handles.f,'Style','slider',...
'Units','Normalized','Position',[0.1 0.15 0.85 0.08],...
'Min',1,'Max',size(data,2),'Value',h,...
'Callback',@(obj,event) slidercallback(obj,'h'));
guidata(handles.f,handles)
function slidercallback(obj,type)
handles=guidata(obj);
data=handles.data;
if strcmp(type,'s')
h=handles.h;
%get list of allowed values and round the current value to that
s_allowed=find(~cellfun('isempty',data(:,h)));
[~,best_match]=min(abs(s_allowed-get(obj,'Value')));
s=s_allowed(best_match);
handles.s=s;guidata(obj,handles)%store new s to guidata
else
s=handles.s;
%get list of allowed values and round the current value to that
h_allowed=find(~cellfun('isempty',data(s,:)));
[~,best_match]=min(abs(h_allowed-get(obj,'Value')));
h=h_allowed(best_match);
handles.h=h;guidata(obj,handles)%store new h to guidata
end
set(handles.plot,'YData',data{s,h});
end
5 Comments
Rik
on 9 Mar 2020
How you get your data into Matlab is up to you. You could do it with xlsread, but I chose to hard-copy it into Matlab variables.
Have you read the documentation for the functions I'm using? The documentation for Matlab is really good and it is worth spending time to understand these functions. They are very versatile and you can use it for many things. These specific vectors describe the positions of each element (I use trial and error to get the positions looking right).
You don't see the value of s and h because they aren't shown anywhere. You can edit the callback function to make sure the values are displayed and updated somewhere. I would suggest putting a text uicontrol element next to each slider. Then you can update the String property (and you can use sprintf to convert the numeric value to a char array).
Rik
on 10 Mar 2020
If my answer solved your problem feel free to mark it as accepted answer. If not, feel free to post your remaining issues.
More Answers (0)
See Also
Categories
Find more on Migrate GUIDE Apps in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!