Slider listener using GUIDE

I need to update the variable frame2display dynamically. But the following code isnt working. How should I modify it? I would like to stick with using GUIDE. Thanks.
function getMCavi_Callback(hObject, eventdata, handles)
handles = guidata(hObject);
[fname,~,~] = uigetfile('*.avi*','Select the motion stablized .avi video',pwd);
VidObj_stbl = VideoReader(fname);
nframes_all=round((VidObj_stbl.Duration)/(1/VidObj_stbl.FrameRate));
for m=1:nframes_all
temp=(readFrame(VidObj_stbl,'native'));
imframes_all(:,:,m)=temp(:,:,1);
end;
imframes_all = double(imframes_all);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%confusing portion %%%%%%%%%%%%%%%%%%%%%%%%%%%
handles.slider_value = addlistener(handles.slider_frameNum,'Value', 'PostSet', ...
@(hObject,eventdata)test2('slider_frameNum_Callback',hObject,eventdata,guidata(hObject)));
slider_value = handles.slider_value;
display(slider_value);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
frame2display = slider_value;
if frame2display == 0
frame2display = 1;
end
img = squeeze(imframes_all(:,:,frame2display));
imagesc(img, 'Parent', handles.MCimages); axis equal tight off; colormap gray
guidata(hObject,handles)
% --- Executes on slider movement.
function slider_frameNum_Callback(hObject, eventdata, handles)
handles = guidata(hObject);
slider_value = get(hObject,'Value');
handles.slider_value = slider_value;
guidata(hObject,handles)

 Risposta accettata

Walter Roberson
Walter Roberson il 9 Mag 2017

0 voti

I would not use addlistner for that. I would use the normal Callback property of the uicontrol('style', 'slider')
You should save imframes_all somewhere, and the Callback for the slider should extract the fetch slider value, pull out the corresponding frame from imframes_all, and image() or imshow() it into the appropriate axes. You would initialize the slider value and call the Callback "manually" to get the process going.

4 Commenti

Dawn
Dawn il 9 Mag 2017
Hi Walter, thanks for the response. However I need more help with the syntax. I dont really understand how to modify the GUIDE template asn shown in my question to extract the slider value.
Walter Roberson
Walter Roberson il 9 Mag 2017
Modificato: Walter Roberson il 9 Mag 2017
function getMCavi_Callback(hObject, eventdata, handles)
handles = guidata(hObject);
[fname,~,~] = uigetfile('*.avi*','Select the motion stablized .avi video',pwd);
VidObj_stbl = VideoReader(fname);
nframes_all=round((VidObj_stbl.Duration)/(1/VidObj_stbl.FrameRate));
for m=1:nframes_all
temp=(readFrame(VidObj_stbl,'native'));
imframes_all(:,:,m)=temp(:,:,1);
end;
handles.imframes_all = double(imframes_all);
set(handles.slider_frameNum, 'Value', 1);
guidata(hObject, handles);
slider_frameNum_Callback(handles.slider_frameNum, [], handles); %trigger a plot
function slider_frameNum_Callback(hObject, eventdata, handles)
handles = guidata(hObject);
slider_value = get(hObject,'Value');
frame2display = max(1, slider_value);
img = squeeze(handles.imframes_all(:,:,frame2display));
ax = handles.MCimages;
imagesc(img, 'Parent', ax);
axis(ax, 'equal', 'tight', 'off');
colormap(ax, gray)
Dawn
Dawn il 9 Mag 2017
THANKS! This works! The only change needed is to round the slider_value so that it can be passed into imframes_all without error.
Just curious, so when is it suitable to use addlistener instead?
Personally I never use addlistener when an existing interface handles the situation. addlistener feels too "backdoor" to me, and it is not at all transparent as to what will happen in any given situation, since a wide variety of different code might have registered listeners for an event.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Creating, Deleting, and Querying Graphics Objects in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by