What is wrong with this uibutton and callback function?

39 ビュー (過去 30 日間)
Stelios Fanourakis
Stelios Fanourakis 2019 年 2 月 9 日
コメント済み: Stelios Fanourakis 2019 年 2 月 11 日
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 件のコメント
Jan
Jan 2019 年 2 月 9 日
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
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 件のコメント
Jan
Jan 2019 年 2 月 11 日
@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
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
Stelios Fanourakis 2019 年 2 月 11 日
@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.

サインインしてコメントする。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by