Slider Code problem with axes

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

You need to clarify exactly why you think that the result is not as expected. In other words, why do you think there is any problem?
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
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.

 Accepted Answer

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

Would this code work ?
function slider2_Callback(hObject, ~, ~)
clc;
load ('100m.mat')
y = (val - 1024 )/200;
Fs = 360;
x = (0:length(y)-1)/Fs ;
z=20;
handles=guidata(hObject);
a=get(handles.slider2,'Value');
set(handles.ax,'XLim',[-0.5 0.5]*handles.z+a)
The way to find out is trying it, but that looks like it should work.
You shouldn't load data from the disk inside a callback function. If you need data, use the guidata struct. Make sure you create the plot outside of the callback and then only change the bare minimum inside the callback. In this case you only need the last 3 lines, as the rest is unused.
If my answer worked for you, please mark it as accepted answer. It will help others with a similar question to find a working solution.
Also, I would discourage the use of GUIDE. It generates code that is bloated and hard to debug, as well as preventing you from using the full potential of programatic creation by all but forcing you to use a .fig file for your GUI.
My small guide to avoid GUIDE:
  • Make a figure (with f=figure;) and look into the doc for figure which properties you want to turn off (you probably want to set Menu and Toolbar to 'none')
  • Create buttons and axes and everything you need with functions like uicontrol and axes. Save the handles to each element to fields of a struct (like handles.mybutton=uicontrol(___);)
  • When you've finished loading all data (and saving it to fields of your handles struct), and creating all the buttons, save your handles struct to the guidata of your figure like this guidata(handles.f,handles);. (You can also use getappdata and setappdata)
  • You can set the Callback property of many objects. If you do, use a function name with an @ in front, or a char array that can be evaluated to valid code. (like @MyFunction or 'disp(''you pushed the button'')')
  • Callback functions will be called with two arguments: the first is a handle to the callback object, the second is eventdata that may contain special information. To get access to your data, just use handles=guidata(gcbo);. You can replace the gcbo function with the name of the first input to your callback function if you prefer.
  • More information about callbacks can be found in multiple places in the doc, for example here.
How can i resize y-axis size for any value to fit the axis ?
Can you write down a code for the slider cause it's still not working and there is no errors ?
it doesn't show the slider in the figure , why ?hello.png
Have you tried stepping through the code with the debugger? I don't see where in this code might be an error. Since you didn't attach the mat file (and posted your code as an image) I can't reproduce your issue.
Here you are , I just want to make the slider work and get limit ranges for y a-axis for any signal to be loaded , i can't figure out what is the problem
You can see a warning displayed:
Warning: 'slider' control requires that 'Min' be less than 'Max'
Control will not be rendered until all of its parameter values are valid
This is because the window size you specified (20) is smaller than the actual x-range of your data (just under 10). There isn't anything to scroll through in that case. You need to either hard-code a window size that makes sense for your use case, or determine the window size programatically. One option would be this:
xwindow_size=0.1*(min(x)-max(x));
You can my example for how to automatically have a good y-scale. Another option would be this:
set(h.ax,'YLim',[floor(min(y)) ceil(max(y))])
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 ;
xwindow_size=1;
%set(h.ax,'YLim',[min(y) max(y)])%fix the y-axis to specific values
set(h.ax,'YLim',[floor(min(y)) ceil(max(y))])%fix the y-axis to specific values
handles=guidata(hObject);
a=get(handles.slider,'Value');
set(handles.ax,'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]);
end
What could be wrong here ?
What h.ax should be replaced with ?
and can i create a code for the slider with its function in a GUI mode ?
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)

Sign in to comment.

More Answers (1)

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.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Tags

Asked:

on 16 Feb 2019

Commented:

Rik
on 20 Feb 2019

Community Treasure Hunt

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

Start Hunting!