# Slider Code problem with axes

1 view (last 30 days)

Show older comments

this an example of a slider to control movement on axes

a = get (handles.slider2,'Value')

x = 0:0.1:50;

y = sin (x*a);

plot (handles.axes1,x,y)

and this me trying to apply the example

function slider2_Callback(hObject, eventdata, handles)

% hObject handle to slider2 (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider

% get(hObject,'Min') and get(hObject,'Max') to determine range of slider

clc;

load ('100m.mat')

a = get(handles.slider2,'Value');

x = 0:0.1:50;

ECGsignal = (val - 1024 )/200;

y = ECGsignal*x*a;

plot (handles.axes1,x,y)

what is the problem in my code ?

##### 4 Comments

Rik
on 18 Feb 2019

### Accepted Answer

Rik
on 19 Feb 2019

Instead of scaling the data, this code changes the axis limits.

%generate some data and set the x window size

x=linspace(0,50,1000);

y=sin(exp(x/10));

xwindow_size=20;

xwindow_min=min(x);

xwindow_max=max(x);

%make the GUI

h=struct;

h.f=figure(1);

clf(h.f)%make sure the figure is empty, not needed if you use h.f=figure;

h.xwindow_size=xwindow_size;

h.xwindow_min=xwindow_min;

h.xwindow_max=xwindow_max;

h.ax=axes('Parent',h.f,...

'Units','Normalized',...

'Position',[0.1 0.3 0.8 0.6]);

h.slider=uicontrol('Parent',h.f,...

'Style','slider',...

'Value',h.xwindow_min+h.xwindow_size/2,...

'min',h.xwindow_min+h.xwindow_size/2,...

'max',h.xwindow_max-h.xwindow_size/2,...

'Units','Normalized',...

'Position',[0.1 0.1 0.8 0.1],...

'Callback',@sliderCallback);

guidata(h.f,h)

%make the plot

plot(x,y,'Parent',h.ax)%create the plot itself

%set(h.ax,'YLim',[min(y) max(y)])%fix the y-axis to specific values

set(h.ax,'YLim',[-1 1])%fix the y-axis to specific values

sliderCallback(h.f)%initialize the x-axis

function sliderCallback(obj,evnt)

handles=guidata(obj);

a=get(handles.slider,'Value');

set(handles.ax,'XLim',[-0.5 0.5]*handles.xwindow_size+a)

end

##### 11 Comments

Rik
on 20 Feb 2019

handles.ax should be replaced by whatever the handle for your axes object is, likely handles.axes1. And again, you have put code in your callback that loads data. That is something you should put in the createFcn of your GUI (or is it called startFcn?). A callback should be treated as an interupt: as little work as possible should be done in them. A callback will trigger often, every 200 ms you spend on loading that mat introduces 200 ms of delay.

% --- Executes on slider movement.

function slider4_Callback(hObject, eventdata, handles)

a=get(handles.slider,'Value');

set(handles.ax,'XLim',[-0.5 0.5]*handles.xwindow_size+a)

### More Answers (1)

Yair Altman
on 17 Feb 2019

In your example, both val and x are not scalars. When you multiply vectors/matrices in Matlab using the * operator, Matlab uses linear algebra rules to multiply the data. This causes an error because the two variables do not match in their size as required by linear algebra multiplication rules (that the number of rows in one multiplicant is equal to the number of columns in the other).

What you probably wanted to do instead was to multiple each element of val by the corresponding element in x (assuming that they are both vectors of the same size) - this is done with element-wise multiplication (.*) :

y = ECGsignal.*x*a;

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!