Using slider to update a variable displayed in a text box
8 ビュー (過去 30 日間)
古いコメントを表示
Khoi Nguyen
2012 年 4 月 15 日
編集済み: Halima Mohamed Ismaeel Hasan Salman Altorabi
2019 年 12 月 25 日
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 件のコメント
採用された回答
Geoff
2012 年 4 月 17 日
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...
その他の回答 (2 件)
Halima Mohamed Ismaeel Hasan Salman Altorabi
2019 年 12 月 25 日
編集済み: Halima Mohamed Ismaeel Hasan Salman Altorabi
2019 年 12 月 25 日
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Graphics Object Properties についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!