What is wrong with this uibutton and callback function?

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.

Answers (2)

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

@Jan.
There are two different images. CroppedImage and J. I want to have two radio buttons and the user to be able to choose one image at a time, for further processing.
I get the error
Undefined function or variable 'hObject'.
Error in ruller (line 143)
guidata(hObject, handles);
>>
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.
Jan
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.

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

@Walter. Do you know the solution to my problem??
If so, please, provide it.
I have told you the solution several times. You need to restructure your code. I described how to structure MATLAB GUI and you said my description was too long.
It looks to me as if you do not want me to provide the solution, but rather that you want me to provide the code. Providing more than a couple of lines of code is not the role of the volunteers here. The role of the volunteers here is to help you understand MATLAB and help you to write your own code.
Perhaps you should have a look at "41 Complete GUI Examples" in the File Exchange to help understand how GUI programming works.
@Walter.
For instance. This one:
function [] = GUI_6()
% Demonstrate how to update one uicontrol with data from others.
% Creates two radiobuttons and a pushbutton. The pushbutton, when clicked
% shows which radio button (or both or none) is currently selected. See
% GUI_8 for similar radiobuttongroup GUI.
%
%
% Author: Matt Fig
% Date: 7/15/2009
S.fh = figure('units','pixels',...
'position',[400 400 120 100],...
'menubar','none',...
'name','GUI_6',...
'numbertitle','off',...
'resize','off');
S.pb = uicontrol('style','push',...
'unit','pix',...
'position',[10 10 100 20],...
'string','None Selected',...
'tooltip','Push to find out which radio button is selected');
S.rd(1) = uicontrol('style','rad',...
'unit','pix',...
'position',[10 40 100 20],...
'string',' Button A');
S.rd(2) = uicontrol('style','rad',...
'unit','pix',...
'position',[10 70 100 20],...
'string',' Button B');
set(S.pb,'callback',{@pb_call,S}); % Set the callback, pass hands.
function [] = pb_call(varargin)
% Callback for pushbutton.
S = varargin{3}; % Get structure.
R = [get(S.rd(1),'val'), get(S.rd(2),'val')]; % Get state of radios.
str = 'Both selected'; % Default string.
if R(1)==1 && R(2)==0
str = 'A selected';
elseif R(1)==0 && R(2)==1
str = 'B selected';
elseif ~any(R)
str = 'None selected';
end
set(S.pb,'string',str)
Where is my output? What do I assign as Img, so Img take the value of one of two images and saves it for further processing?
I don't understand the output variable in this code I attached from "41 Complete GUI Examples"
Can you help?
That GUI_6 does not create any output variables. It displays the output as the text displayed on the pushbutton.
It will be easier for you to understand if you give up on the idea that GUI functions create "output" other than displaying things or writing to files. Instead, GUI functions change the state of something that your other GUI functions know to look at.
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()
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.

Categories

Find more on Scripts 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!