Clear Filters
Clear Filters

What is wrong with this uibutton and callback function?

8 views (last 30 days)
I want to have two radio buttons pick one image at a time for further processing. I use this code.
bg = uibuttongroup('Visible','off',...
'Position',[0 0 .2 1],...
r1 = uicontrol(bg,'Style',...
'String','Option 1',...
'Position',[10 350 100 30],...
r2 = uicontrol(bg,'Style','radiobutton',...
'String','Option 2',...
'Position',[10 250 100 30],...
% Make the uibuttongroup visible after creating child objects.
bg.Visible = 'on';
Img = bg; %%% This is the image I need to define whether Option1 = croppedImage, or Option2 = J; CroppedImage and J are two different images.
And the callback function at the end of the script.
function Do_plot(hObject, event, varargin)
bg = findobj(gcf, 'Tag', 'BG1');
sel = bg.SelectedObject;
if isempty(sel)
return; %no buttons selected
sel_string = sel.String;
switch sel_string
case 'Option 1'
bg.Value = croppedImage;
case 'Option 2'
bg.Value = J;
bg= double(bg);
I cannot make it run.
Is there any incongruency?
  1 Comment
Jan on 9 Feb 2019
Edited: Jan on 9 Feb 2019
Please mention the details. "Cannot make it run" does not explain, what you observe.
By the way, why using an expensive search, when the handle is stored already:
function Do_plot(hObject, event, handles) % instead of: varargin
bg = handles.BG1; % or how itr is called findobj(gcf, 'Tag', 'BG1');
This is strange:
Img = bg; %%% This is the image I need to define
No, bg is the handle of the button group, not an image.

Sign in to comment.

Answers (2)

Jan on 9 Feb 2019
I guess, that the object with the tag 'BG1' is not found. Then store it explicitly:
bg = uibuttongroup('Visible','off',...
'Position',[0 0 .2 1],...
'SelectionChangeFcn',@Do_plot, ...
'Tag', 'BG1');
handles.BG1 = bg;
guidata(hObject, handles);
But the rest is not clear: What do you want to achieve? Radiobuttons are mutual exclusive, usually. Have you implemented this already?
Walter Roberson
Walter Roberson on 10 Feb 2019
You have given us no information as to what phi is at that point.
Jan on 11 Feb 2019
Edited: Jan on 11 Feb 2019
@Stelios: Without knowing the code of the function ruller, I cannot guess, how to avoid the error message about the undefined variable hObject. I do not see the relation between the original question and a black image.
Again: "What is wrong with this uibutton and callback function?" does not contain any information about why you assume, that there is a problem. The comments you have given do not clarify this also. So what exactly is teh problem you want to solve?

Sign in to comment.

Walter Roberson
Walter Roberson on 9 Feb 2019
Edited: Walter Roberson on 9 Feb 2019
bg= double(bg);
At that point in the code, bg is the handle of a uibuttongroup . When you apply double() to the handle of a graphics object, the result you get back is the old numeric style handle for the object:
>> bg = uibuttongroup('Visible','off',...
'Position',[0 0 .2 1],...
>> double(bg)
ans =
>> handle(ans)
ans =
ButtonGroup with properties:
Title: ''
BackgroundColor: [0.9400 0.9400 0.9400]
SelectedObject: [0×0 GraphicsPlaceholder]
SelectionChangedFcn: @Do_plot
Position: [0 0 0.2000 1]
Units: 'normalized'
Show all properties
This has nothing to do with any setting chosen by way of the button group. It is like a street address of where the button group "lives".
So you are replacing bg with the numeric handle to the button group . That is valid to do, but not recommended.
After you replace bg with the numeric handle to the button group, your callback returns without having stored any information permanently. You might as well not have run the code, if you are not going to store anything. You should be reading Share about how to store results that survive the callback.
But before that you have a problem: bg.Value = croppedImage; and bg.Value = J are not valid because uibuttongroups do not have any property named Value that can be assigned into.
... But I have explained all of this to you several times before. Graphics callbacks are not subroutines, and graphics objects do not return values.
Stelios Fanourakis
Stelios Fanourakis on 11 Feb 2019
May you know why I cannot access the path
Using Matlab from my Macbook??
It's just that when I click to choose the folder, from where Matlab should read the functions, all the path from Applications and onwards is just faded with no ability to chose.

Sign in to comment.


Find more on Migrate GUIDE Apps 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!