Using slider to update a variable displayed in a text box

8 Ansichten (letzte 30 Tage)
Hi, I am writing a GUI code to plot rainfall of a given year. On the figure, I use a slider to allow the user to view the data at different times, and a text box to shows the starting and ending time of the period currently shown on the screen. I am unsure how to use the slider to update the date displayed in the text box. I am new to Matlab GUI, and would much appriciate if anyone can help with this problem. The code I got so far is as followed, and it does not the required task. Please help me because I really need it for my final thesis
function scrollplot3(dx,x)
a=gca;
data = guidata(a);
data.dx = dx;
stepratio = .1;
xmax = max(x);
xmin =min(x);
data.xmin = xmin;
data.xmax = max(xmax, xmin + dx);
data.dx = dx;
center = xmin;
set(gcf,'doublebuffer','on'); pos = get(a,'position'); pos = [pos(1) pos(2)-0.1 pos(3) 0.05];
S=['set(gca,''xlim'',get(gcbo,''value'')+[0 ' num2str(dx) '])'];
steptrough = dx / (data.xmax - data.xmin - dx);
steparrow = stepratio * steptrough;
a = uicontrol('style', 'slider',... 'units', 'normalized', 'position', pos, ... 'callback', S, ... 'min', data.xmin, 'max', data.xmax - dx, 'value', data.xmin, 'sliderstep',[steparrow steptrough]);
handles.text=uicontrol('Style','text',...
'Position',[200 65 300 20],...
'String',[num2str(datestr(data.xmin,'dd-mmm HH:MM:SS')),' ', num2str(datestr(data.xmax,'HH:MM:SS'))],'FontSize',12)
set(gca, 'xlim', center + [0 data.dx]);

Akzeptierte Antwort

Geoff
Geoff am 17 Apr. 2012
In your slider-change callback, you need to add code that will cause the edit box to update.
function onSliderChanged
udata = get( gcbf, 'UserData' );
dx = udata.data.dx;
val = get(gcbo,'value') + [0 ' num2str(dx) ']; % What???
set( gca, 'xlim', val ); % Beware gca might not be correct axes
updateEditBox( val );
end
function updateEditBox( val )
udata = get( gcbf, 'UserData' );
h = udata.handles.hEdit;
dx = udata.data.dx;
datefrom = datestr(val,'dd-mmm HH:MM:SS');
dateto = datestr(val+dx,'HH:MM:SS');
set(h, 'string', [datefrom, ' ', dateto], 'FontSize', 12);
end
Notes:
1) I assume you've stored your edit box handle in a structure into the user data for your GUI figure.
2) I took away the num2str calls that you had wrapped around your datestr calls.
3) I made a 'onSliderChanged' callback function instead of putting the code directly into S (the variable you use when you create your slider).
4) gca might not point to the axes that you expect during callback. You should store a handle to your axes with your GUI user data.
Something like that anyway...
  1 Kommentar
Khoi Nguyen
Khoi Nguyen am 17 Apr. 2012
Really appreciate your help Geoff. I am trying to incorporate your changes into my existing code. Hopefully it will work.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Khoi Nguyen
Khoi Nguyen am 17 Apr. 2012
Hi Jan, the code I got above is able to display a slider and it also allows me to roll over the entire figure using that slider. At each particular slider position, the starting and ending time of the figure section currently shown on the screen suppose to be displayed in the text box. However, the code I provided does not change the date in the box when I move the slider. I just slightly modified it as followed, but it still does not work. I would much appreciate if you can help me out with this problem Jan.
function scrollplot3(dx,x)
a=gca;
data = guidata(a);
data.dx = dx;
stepratio = .1;
xmax = max(x);
xmin =min(x);
data.xmin = xmin;
data.xmax = max(xmax, xmin + dx);
data.dx = dx;
center = xmin;
set(gcf,'doublebuffer','on');
pos = get(a,'position');
pos = [pos(1) pos(2)-0.1 pos(3) 0.05];
S=['set(gca,''xlim'',get(gcbo,''value'')+[0 ' num2str(dx) '])'];
steptrough = dx / (data.xmax - data.xmin - dx);
steparrow = stepratio * steptrough;
%This step is to add a slider into the current figure
a= uicontrol('style', 'slider','units', 'normalized', 'position', pos,'callback', S, ... 'min', data.xmin, 'max', data.xmax - dx, 'value',... data.xmin, 'sliderstep',[steparrow steptrough]);
%This step is add a text box to show the current time of the figure section currently displayed on the screen
h=uicontrol('Style','text',...
'Position',[200 65 300 20],'string',0,'callback',@edit_val);
set(gca, 'xlim', center + [0 data.dx]);
% This step is to get the current date value from the slider
function edit_val
L=get(a,'value')
set(h,'string',[num2str(datestr(L,'dd-mmm HH:MM:SS')),' ', num2str(datestr(L+dx,'HH:MM:SS'))],'FontSize',12);

Halima Mohamed Ismaeel Hasan Salman Altorabi
Bearbeitet: Halima Mohamed Ismaeel Hasan Salman Altorabi am 25 Dez. 2019
This may help others who wants to update the text in a plot figure
I did it with help from this demo:
You can update the text appears in the scrollable plot figure after the plot command, by storing the reqired values in array with specifying the x and y dimentions
The text will appear on a specific points in the figure. The points are specified by"ennnd" array and the text is in "maxxx" array.
you can insted of text comand implement "annotation" command.
dx=7;
x=time;
y=signal;
a=gca;
p=plot(x,y);
beginnn=[];
ennnd=[];
maxxx=[];
[beginnn,ennnd,maxxx]=labelData(y,fs);
text(x(ennnd),y(ennnd),maxxx,'FontSize',16)
xlabel('Sec')
ylabel( 'mV');
title('ECG Raw data');
% Set appropriate axis limits and settings
set(gcf,'doublebuffer','on');
set(gcf, 'units','normalized','outerposition',[0 0 1 0.9]);
%% This avoids flickering when updating the axis
set(a,'xlim',[0 dx]);
set(a,'ylim',[min(y) max(y)]);
% Generate constants for use in uicontrol initialization
pos=get(a,'position');
Newpos=[pos(1) pos(2)-0.1 pos(3) 0.05];
%% This will create a slider which is just underneath the axis
%% but still leaves room for the axis labels above the slider
xmax=max(x);
S=['set(gca,''xlim'',get(gcbo,''value'')+[0 ' num2str(dx) '])'];
%% Setting up callback string to modify XLim of axis (gca)
%% based on the position of the slider (gcbo)
% Creating Uicontrol
h=uicontrol('style','slider',...
'units','normalized','position',Newpos,...
'callback',S,'min',0,'max',xmax-dx);

Kategorien

Mehr zu Graphics Object Properties finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by