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 个评论
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.
回答(2 个)
Jan
2019-2-9
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 个评论
Stelios Fanourakis
2019-2-9
Stelios Fanourakis
2019-2-9
Walter Roberson
2019-2-10
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?
Walter Roberson
2019-2-9
编辑:Walter Roberson
2019-2-9
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 个评论
Stelios Fanourakis
2019-2-9
Walter Roberson
2019-2-10
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.
Stelios Fanourakis
2019-2-10
Walter Roberson
2019-2-10
That GUI_6 does not create any output variables. It displays the output as the text displayed on the pushbutton.
You should read Sharing data between callbacks
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()
Stelios Fanourakis
2019-2-11
Jan
2019-2-11
which menu.m
Stelios Fanourakis
2019-2-11
类别
在 帮助中心 和 File Exchange 中查找有关 Scripts 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!