MATLAB Answers

0

What is wrong with this uibutton and callback function?

Asked by Stelios Fanourakis on 9 Feb 2019
Latest activity Commented on by Stelios Fanourakis on 11 Feb 2019
Hi
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],...
'SelectionChangeFcn',@Do_plot);
r1 = uicontrol(bg,'Style',...
'radiobutton',...
'String','Option 1',...
'Position',[10 350 100 30],...
'HandleVisibility','off');
r2 = uicontrol(bg,'Style','radiobutton',...
'String','Option 2',...
'Position',[10 250 100 30],...
'HandleVisibility','off');
% 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
end
sel_string = sel.String;
switch sel_string
case 'Option 1'
bg.Value = croppedImage;
case 'Option 2'
bg.Value = J;
end
bg= double(bg);
end
I cannot make it run.
Is there any incongruency?

  1 Comment

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.

2 Answers

Answer by 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?

  4 Comments

Show 1 older comment
I also get a black image instead of the image I was supposed to get and the error
Error using contour (line 48)
Z must be at least a 2x2 matrix.
Error in ruller (line 193)
hold on,[c,h] = contour(real(phi),[0 0],'r','linewidth',1); hold off
>>
I know that it is not directly related to this command I am trying to develop, but it must have a connection somehow, ;cause it's the first time I get error at that line, and it came after the new lines I added for the uicontrols and uibuttons
You have given us no information as to what phi is at that point.
@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.


Answer by Walter Roberson
on 9 Feb 2019
Edited by 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],...
'SelectionChangeFcn',@Do_plot);
>> double(bg)
ans =
0.0003662109375
>> 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.

  7 Comments

If you find that you cannot give up on the idea that GUI functions create "output" then study local_GUImenu inside menu.m, or study the longer inputdlg()
@Walter
Where is local_GUImenu, where is menu.m??
@Jan
May you know why I cannot access the path
/Applications/MATLAB_R2018b.app/toolbox/matlab/uitools/menu.m
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.