Search code examples
matlabmatlab-figure

How to use slider on graph plot in matlab


I am using an audioread to plot audio signal of an audio file.What I want is to use a slider along the x-axis of the plot so I can see the complete image of the plot more clearly by expanding it horizontally in the same amount of space

I have written the following code, please help with how a slider could be added in the code:

function upload_Callback(hObject, eventdata, handles)
filename = uigetfile({'*.mp3';'*.wav';'*.wma';'*.ogg';'*.*'},'File Selector');
handles.filename=filename;                      
guidata(hObject,handles);
[y,fs] = audioread(filename);
dt = 1/fs;
t = 0:dt:(length(y)*dt)-dt;

N=length(y);
slength=N/fs;

handles.t=slength;

set(handles.audio_duration,'String',num2str(handles.t));

plot(handles.audio_signal,t,y);
xlabel(handles.audio_signal,'')
ylabel(handles.audio_signal,'Amplitude')

I want the x axis to be 50s and have a scroll so I can see the results of a 200s audio file clearly 50s at a time in the same space already alloted to the graph


Solution

  • The following code is just a snipped, but I think you get the idea. Just place a uicontrol-element (slider) into your gui and attach a callback function. If you move that slider, the callback-function will be executed and you can use the value from your slider to set the axis-limits.

    h = uicontrol('style','slider','position',[100 250 300 20],'min',0,'max',5,'callback',@fct);
    
    function fct(source,eventdata)
    
        n=get(h,'value');
        plot(x,y);
        xlim([0 n])
    end