MATLAB Answers

0

Just a small modification to go please

Hi
I have modified my code from previous times. I am so close to achieve what I want to achive but I know that I cannot surpass some minor obstacles. Can you please help me. I want to have the user being able to choose between two images.
I use that code.
S.bg = uibuttongroup(gcf,'Visible','off',...
'Position',[0 0 .2 1]);
S.r(1) = uicontrol(S.bg,'Style',...
'radiobutton',...
'String','Option 1',...
'Position',[10 350 100 30]);
S.r(2) = uicontrol(S.bg,'Style','radiobutton',...
'String','Option 2',...
'Position',[10 250 100 30]);
S.pb = uicontrol('style','push',...
'unit','pix',...
'position',[75 20 100 30],...
'string','Get Current Radio',...
'callback',{@pb_call,S});
handles.axes1 = subplot(1,2,1)
handles.axes2 = subplot(1,2,2)
guidata(handles.axes1,croppedImage);
guidata(handles.axes2,J);
Img = double(S.bg);
setappdata(gcf,'Img',Img);
ANd the callback function at the end
function [] = pb_call(varargin)
getappdata(gcf,'Img',Img);
% Callback for pushbutton.
S = varargin{3}; % Get the structure.
% Instead of switch, we could use num2str on:
% find(get(S.bg,'selectedobject')==S.rd) (or similar)
% Note the use of findobj. This is because of a BUG in MATLAB, whereby if
% the user selects the same button twice, the selectedobject property will
% not work correctly.
switch findobj(get(S.bg,'selectedobject'))
case S.rd(1)
set(S.ed,'string','1') % Set the editbox string.
Img = getimage(handles.axes1, croppedImage)
case S.rd(2)
set(S.ed,'string','2')
Img = getimage(handles.axes2, J)
otherwise
set(S.ed,'string','None!') % Very unlikely I think.
end
guidata(gcf,S,Img);
drawnow()
end
I get a black image and if I display the variable Img I get just a number
>> Img
Img =
2.0289306640625
So, what is wrong? Can you please help?
I am very close to achieve it. I can feel it, but, obviously I have a lack of some basics knowledge.
PLease, help!!!!

  0 Comments

Sign in to comment.

1 Answer

Answer by Walter Roberson
on 13 Feb 2019
 Accepted Answer

getimage expects either no arguements or a single graphics handle . It does a findobj 'type' 'image' and extracts the cdata of the first one it finds. You should not be passing two arguments .
You did not image() or imagesc() or imshow() in those axes handles so getimage would return empty .
guidata(object, data) chases the given object through the graphics hierarchy to find the parent figure and replaces all of the figure's gui data with the given data. The gui data for a figure is stored as one of the properties of the figure by way of setappdata so guidata is aa shortcut for setappdata . Convention has guidata used with a struct but that is not strictly necessary .
You call guidata twice with different axes handles . However they belong to the same figure so they refer to the same storage area so the second one is overwriting the first . However you never use the guidata so it does not matter .
I suspect that instead of using guidata there you want to display the images in the axes so that you can get them later with getimage .
guidata inside your callback is wrong because guidata cannot take 3 arguements .
You could
S.Img = Img;
guidata(varargin{1}, S)
and later use guidata to retrieve the updated S and get the Img field from it.
One thing to be cautious about is that when you create your callback during your assignment to S.pb then the S that will be recorded in {@pb_call,S} will be the S that exists at the start of the assignment before the assignment that creates S.pb is done . Therefore the recorded S will not include the pb field . It is safer to go through creating all of the objects in the struct without any callback settings first and only after all the struct fields have been filled in, go back through and set the callback properties .
As for why double(S.bg) fails, I have explained repeatedly that double applied to a graphics handle gives the old style numeric graphics handle , sort of like a street address . You are storing the results of the callback using guidata which is not going to associate it with any part of the buttongroup.

  3 Comments

I tried this
S.Img = Img;
guidata(varargin{1}, S.Img)
Undefined variable "varargin" or class "varargin".
Error in ruller (line 177)
guidata(varargin{1}, S.Img)
Still, some errors
Can you please give me an example of what you mean by creating them in advance, without callbacks and later on stick the callback.
I cannot imagine. I just follow some ready examples and try to figure out how to solve it.
See attached.
Thank you Walter. Very helpful and very inspirational. You pulled me out of a really stressful situation.
Cheers!

Sign in to comment.