Using values from a slider

2 vues (au cours des 30 derniers jours)
Bran
Bran le 18 Mar 2015
Modifié(e) : Mischa Kim le 18 Mar 2015
I have a slider and a plot which automatically updates as I move the slider however depending on where I place the sliders I would like the values to create a subset of my dataset. I am confused regarding hwo the sliders work and how to go about this. I thought that I simply needed to use n2 =
value1 = get(slider1,'Value');
value2 = get(slider2,'value');
at the end of my function and then
simply create a new data = Olddata(Value1:Value2).
However, I get errors which say that Value1 and Value2 are undefined. How does this work because I have nested functions does it mean that every time I move my slider this new subset needs to be updated. How can I do this?
function sliderattempt
global Datasetnew
MM = load('Datasetold.txt');
X = MM(:,2);
f = figure('Visible','off');
hAxes = axes('Position',[.1 .15 .8 .8]);
sldplot=plot(X);
x_limit = get(gca,'XLim');
y_limit = get(gca,'YLim');
n = numel(X);
s1 = uicontrol('Style', 'slider','Min',1,'Max',n,'Value',1,'Position', [100 5 120 20],'Callback', @s1_Callback);
s2 = uicontrol('Style', 'slider','Min',1,'Max',n,'Value',n,'Position', [400 5 120 20],'Callback', @s2_Callback);
txt = uicontrol('Style','text','Position',[400 30 120 15],'String','Second');
txt2 = uicontrol('Style','text','Position',[100 30 120 15],'String','First');
addlistener(s1, 'ContinuousValueChange', @S1ListenerCallback);
addlistener(s2, 'ContinuousValueChange', @S2ListenerCallback);
set(f,'Visible','on');
function S1ListenerCallback(source,callbackdata)
n2 = round(get(s1,'Value'));
n3 = round(get(s2, 'Value'));
set(sldplot,'xdata',(n2:n3),'ydata',X(n2:n3));
set(gca,'XLim',x_limit,'YLim',y_limit)
drawnow;
end
function s1_Callback(source,callbackdata)
n2 = round(get(s1,'Value'));
n3 = round(get(s2, 'Value'));
set(sldplot,'xdata',(n2:n3),'ydata',X(n2:n3));
set(gca,'XLim',x_limit,'YLim',y_limit)
drawnow;
end
function S2ListenerCallback(source,callbackdata)
n2 = round(get(s1,'Value'));
n3 = round(get(s2, 'Value'));
set(sldplot,'xdata',(n2:n3),'ydata',X(n2:n3));
set(gca,'XLim',x_limit,'YLim',y_limit)
drawnow;
end
function s2_Callback(source,callbackdata)
n2 = round(get(s1,'Value'));
n3 = round(get(s2, 'Value'));
set(sldplot,'xdata',(n2:n3),'ydata',X(n2:n3));
set(gca,'XLim',x_limit,'YLim',y_limit)
drawnow;
end
Datasetnew = Datasetold(n2:n3);
end

Réponses (0)

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by