Using slider to update a variable displayed in a text box
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Khoi Nguyen
le 15 Avr 2012
Modifié(e) : Halima Mohamed Ismaeel Hasan Salman Altorabi
le 25 Déc 2019
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]);
1 commentaire
Réponse acceptée
Geoff
le 17 Avr 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...
Plus de réponses (2)
Halima Mohamed Ismaeel Hasan Salman Altorabi
le 25 Déc 2019
Modifié(e) : Halima Mohamed Ismaeel Hasan Salman Altorabi
le 25 Déc 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);
0 commentaires
Voir également
Catégories
En savoir plus sur Graphics Object Properties dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!