我有一个滑块和一个编辑字段,定义为
uicontrol(fig,'Style','Slider','Units','characters','Position',[17.1+f*iwidth 10.5 4 28.6],'Min',0,'Max',1000,'Value',500,'SliderStep', [1/500 , 10/500 ],'Callback','evaluation_callbacks(''results'',guidata(gcbo))','Tag',['slider' int2str(f)]);
uicontrol(fig,'Style','Edit','Enable','inactive','Units','characters','Position',[14+f*iwidth 8.2 9 1.6],'FontSize',10,'String',500,'Tag',['rating' int2str(f)]);
使用以下回调函数:
function evaluation_callbacks(varargin)
% EVALUATION_CALLBACKS Callback functions for the evaluation interface
%
% evaluation_callbacks(fname,varargin) executes the callback function
% fname_callback with various parameters
fname=[varargin{1},'_callback'];
feval(fname,varargin{2:end});
%%%saving the rating results and proceeding to the next experiment or exiting
function results_callback(handles)
% stop audio
clear sound
%getting the ratings for all files
for f=1:handles.nbfile
handles.ratings(handles.expe_order(handles.expe),handles.file_order(f))=get(getfield(handles,['slider' int2str(f)]),'Value');
end
%saving the whole results (to avoid losing data if the program terminates early)
results='';
fid=fopen(handles.resultfile,'w');
for e=1:handles.nbexpe
for f=1:handles.nbfile
fprintf(fid,'%d\n',handles.ratings(e,f));
end
end
fclose(fid);
if handles.expe<handles.nbexpe
handles.expe=handles.expe+1;
% updating title
set(handles.experiment,'String',handles.parameter{handles.expe});
% update evaluation parameters
set(handles.scale90,'String',handles.high{handles.expe});
set(handles.scale10,'String',handles.low{handles.expe});
if handles.expe==handles.nbexpe
pos=get(handles.results,'Position');
pos(1)=pos(1)+2.5;
pos(3)=19;
set(handles.results,'Position',pos,'String','Save and exit');
end
%moving all the sliders back to 50
for f=1:handles.nbfile
shandle=getfield(handles,['slider' int2str(f)]);
set(shandle,'Value',500);
rhandle=getfield(handles,['rating' int2str(f)]);
set(rhandle,'String',500);
end
%randomizing the order of the tested files for the next experiment
handles.file_order=randperm(handles.nbfile);
%testing whether a break is needed before the next experiment
if etime(clock,handles.time) > 20*60
wfig=warndlg(['You have been working for ' int2str(round(etime(clock,handles.time)/60)) 'minutes. It is recommended that you take a break of at least the same duration before starting the next experiment. Click on OK when you are ready.'],'Warning');
uiwait(wfig);
end
handles.time=clock;
% Start next audio sample
play_callback(handles,1)
guidata(gcbf,handles);
else
%exiting
close(gcbf);
end
%%%rounding and displaying the values of the sliders
function slider_callback(handles,f)
shandle=getfield(handles,['slider' int2str(f)]);
set(shandle,'Value',round(get(shandle,'Value')));
rhandle=getfield(handles,['rating' int2str(f)]);
set(rhandle,'String',get(shandle,'Value'));
就像现在一样,编辑字段值不是实时更新的。仅当移动滑块并释放鼠标按钮时,才会更新编辑字段的值。
我试图在滑块上添加一个监听器,但我无法使它工作。到目前为止,我一直遵循以下指南/帖子,没有任何运气。
http://undocumentedmatlab.com/blog/continuous-slider-callback 和 https://se.mathworks.com/matlabcentral/answers/140706-use-slider-for-live-updating-of-threshold-on-2d-contour-plot
有人可以帮忙吗?
答案 0 :(得分:1)
在myGUI_OpeningFcn
中添加一个监听器。请注意,您需要更改myGUI
以匹配您的GUI脚本文件名。
addlistener(handles.slider1,'ContinuousValueChange',@(hObject,eventdata)myGUI('updateText',hObject,eventdata,guidata(hObject)));
然后,在GUI脚本中添加以下功能。
function updateText(hObject, eventdata, handles)
set(handles.text2,'String',get(hObject,'Value'));
guidata(hObject,handles);
更具体地说,在您的情况下,您需要添加一堆侦听器:
for i=1:handles.nbfile
addlistener(getfield(handles,['slider' int2str(i)]),'ContinuousValueChange',@(hObject,eventdata)myGUI('updateText',hObject,eventdata,guidata(hObject)));
end
然后添加:
function updateText(hObject, eventdata, handles)
f = hObject.Tag(7:end);
set(getfield(handles,['rating' f]),'String',get(hObject,'Value'));
guidata(hObject,handles);
<强>更新强>
在GUI脚本中,在OpeningFcn
中添加侦听器(这是创建GUI时由Matlab自动生成的第二个函数):
for i=1:handles.nbfile
addlistener(getfield(handles,['slider' int2str(i)]),'ContinuousValueChange',@(hObject,eventdata)myCallBackFileName('updateText',hObject,eventdata,guidata(hObject)));
您需要更改myCallBackFileName
以匹配回调脚本的文件名。然后添加updateText函数。