How to have slider gui only return after button push?
Show older comments
Hi all! I created a function to create a interactive dual slide image thresholder based on an upper and lower grayscale limit. I want the upper and lower limits to return to the .m file after a pushbutton is pushed. Right now its just returing the default values, and not waiting for the pushbutton to work. Any ideas for how I can get it to wait for the user to define when to return the values of interest?
function [x, y, buttonPush] = slideThresh(photo)
sThresh = photo;
FigH = figure('Position', [ 0 0 700 800 ]);
axes1 = axes('Position', [.125 0 .75 .75]);
imshow(sThresh, 'Parent', axes1);
leaveMe = uicontrol('style', 'pushbutton', 'string', 'Done', 'CallBack', @buttonPushed);
% leaveMe.Callback = @buttonPushed;
% x = linspace(0, 255, 255000);
annotation('textbox', [.423 .75 .1 .1], 'String', 'Upper Threshold', 'FitBoxToText', 'on')
annotation('textbox', [.423 .625 .1 .1], 'String', 'Lower Threshold', 'FitBoxToText', 'on')
TextH = uicontrol('style','text',...
'position',[325 625 40 15]);
SliderH = uicontrol('style','slider','Value', 255, 'position',[50 600 600 20],...
'min', 0, 'max', 255);
TextD = uicontrol('style','text',...
'position',[325 525 40 15]);
SliderD = uicontrol('style','slider','Value', 0, 'position',[50 500 600 20],...
'min', 0, 'max', 255);
addlistener(SliderH, 'Value', 'PostSet', @callbackfn);
addlistener(SliderD, 'Value', 'PostSet', @callbackfn1);
movegui(FigH, 'center')
num1 = 255;
num2 = 0;
buttonPush = 0;
function callbackfn(source, eventdata)
num1 = get(eventdata.AffectedObject, 'Value');
TextH.String = num2str(num1);
sNew = (sThresh < num1) & (sThresh > num2);
imshow(sNew, 'Parent', axes1);
end
function callbackfn1(source,eventdata)
num2 = get(eventdata.AffectedObject, 'Value');
TextD.String = num2str(num2);
sNew = (sThresh < num1) & (sThresh > num2);
imshow(sNew, 'Parent', axes1);
end
function buttonPushed(source, event)
buttonPush = 1;
end
x = get(SliderH, 'Value');
y = get(SliderD, 'Value');
end
3 Comments
Adam
on 18 Jun 2019
doc uiwait
should do the job, either with a paired
doc uiresume
or you may just be able to give the figure handle to the uiwait call and that will be enough so long as you explicitly call delete on the figure in your pushbutton callback.
Adam Danz
on 18 Jun 2019
"How to have slider gui only return after button push?"
Instead of using a slider callback function, just use the pushbutton callback function that references the current slider value.
I think the initial problem is that the function simply runs to completion, creating the UI and returning its output arguments without any opportunity in the middle of that for the user to actually use the GUI.
Though I agree that, having solved that issue, it is not generally useful to have a slider callback constantly updating some variable that isn't used until you press some other button.
Using the slider callback only makes sense when you are actually doing something in response to the slider, such as updating a plot or some other UI element.
Answers (0)
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!