MATLAB Answers


Slider Code problem with axes

Asked by Hassan Bosha on 16 Feb 2019
Latest activity Commented on by Rik
on 20 Feb 2019
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
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 ?


Show 1 older comment
ECGsignal is a matrix signal and i need to connect it with a and x as the first example
the attached photo display what occurs when i move the slidermat.png
on 18 Feb 2019
If you want to scroll through your data, why don't you use the Value property of the slider to change the XLim property of your axes object?
can you show me a example code for it ?

Sign in to comment.


2 Answers

Answer by Rik
on 19 Feb 2019
 Accepted Answer

Instead of scaling the data, this code changes the axis limits.
%generate some data and set the x window size
%make the GUI
clf(h.f)%make sure the figure is empty, not needed if you use h.f=figure;
'Position',[0.1 0.3 0.8 0.6]);
'Position',[0.1 0.1 0.8 0.1],...
%make the plot
plot(x,y,'Parent', the plot itself
%set(,'YLim',[min(y) max(y)])%fix the y-axis to specific values
set(,'YLim',[-1 1])%fix the y-axis to specific values
sliderCallback(h.f)%initialize the x-axis
function sliderCallback(obj,evnt)
set(,'XLim',[-0.5 0.5]*handles.xwindow_size+a)


That's perfect , all i need is to fit it on the my GUI Code , any idea how ?
% --- Executes on slider movement.
function slider4_Callback(hObject, eventdata, handles)
load ('100m.mat')
y = (val - 1024 )/200;
Fs = 360;
x = (0:length(y)-1)/Fs ;
%set(,'YLim',[min(y) max(y)])%fix the y-axis to specific values
set(,'YLim',[floor(min(y)) ceil(max(y))])%fix the y-axis to specific values
set(,'XLim',[-0.5 0.5]*handles.xwindow_size+a)
% --- Executes during object creation, after setting all properties.
function slider4_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
What could be wrong here ?
What should be replaced with ?
and can i create a code for the slider with its function in a GUI mode ?
on 20 Feb 2019 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)
set(,'XLim',[-0.5 0.5]*handles.xwindow_size+a)

Sign in to comment.

Answer by 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;

  1 Comment

still same problem , i just need a code to move the slider on zooming

Sign in to comment.