MATLAB Answers

0

How to make an interactive panel for the user to chose among several options

Asked by Stelios Fanourakis on 2 Feb 2019
Latest activity Commented on by Walter Roberson
on 9 Feb 2019
Hi
I want to have a panel (User Interface) with 3-4 different cropping image options and the user shall be able to chose the size he likes.
For instance, I could have subplot(2,2,1-4) four different cropping window sizes, of an image.
For example, a big cropping area of the image, a smaller one by 30%, an even smaller one by 50% of the original image, and a finally a very thin and slight strip of image.
By mouse clicking, the user can chose the appropriate cropping image window size for further processing.
Is it possible to do that?
Thanks

  0 Comments

Sign in to comment.

1 Answer

Answer by Walter Roberson
on 2 Feb 2019
 Accepted Answer

Yes. You could configure uicontrol style 'radio' in a uibuttongroup, or you could configure a single uicontrol style 'listbox' or 'popup' that had the four options configured.

  21 Comments

The major problem is that you are not understanding the architecture of GUI systems.
GUI elements are mostly asynchronous. You do not create a GUI element and then expect it to return a value by referring to it right afterwards: GUI elements are not subroutines. You need to provide an opportunity for the user to interact with the element, and for the user to trigger an event that your code reacts to through a callback.
  1. create graphics elements (possibly by loading .fig)
  2. if you have a backing MATLAB interactive session, you can return to the MATLAB prompt at this point if you want, leaving the GUI figure up; otherwise, uiwait() or waitfor() the GUI figure to be closed, after which you would close the GUI figure
  3. User moves and clicks and whatever, eventually triggering a callback that you have configured. Each callback runs, does whatever is appropriate, and the returns back to whatever the program was doing (possibly waiting, possibly processing a different routine.)
  4. if the callback is for chosing settings, then do whatever validation needed and when satisfied, either leave the GUI elements configured that way to be retrieved later or else store the results into a location you can get at afterwards; Share . Sometimes it makes sense that choosing a setting should enable further GUI elements, so you might either create those elements or have set them to be visible or enabled if you had created them before. For example if you were doing a polynomial fitting routine, then the 'FIT' action button should be disabled until the data has been chosen and the user has entered a polynomial degree.
  5. if the callback is for chosing files, then do whatever validation is needed and when satisfied, either leave the GUI elements configured that way to be retrieved later or else store the file name in a location you can get at afterwards or else load the file and store the useful information from it into a location you can get at afterwards, possibly displaying something as you go; Share. Sometimes it makes sense that choosing a file should enable further GUI elements, so you might either create those elements or have set them to be visible or enabled if you had created them before. For example if you were doing a polynomial fitting routine, then the 'FIT' action button should be disabled until the data has been chosen and the user has entered a polynomial degree.
  6. if the callback is something you have designed to signal that action should be taken, then gather the necessary settings and data from GUI elements or from the information you saved in the other callbacks, and process the data, outputting appropriate results, and possibly saving intermediate results into locations that you can get at afterwards; Share . Sometimes it makes sense that performing an action should enable further GUI elements, so you might either create those elements or have set them to be visible or enabled if you had created them before. For example if you were doing a polynomial fitting routine, then a request to display an optional error plot should be disabled until the user has run the fitting; if you were doing steganography, then the "Extract" action should possibly be disabled until the "Embed" action has been run (*or* the user has done a Load Embedded Image action.)
There is a bit of room for variation. An executing program can force a graphics interaction to be done in sequence by:
  1. create graphics elements inside a figure that was created as 'WindowStyle', 'modal', or make pre-created graphics elements visible
  2. uiwait() or waitfor() that figure to close or for a particular property of a graphics element to change or to become a particular value. With the 'modal' figure in effect, the user will not be permitted to interact with any other figure in the mean time
  3. destroy the graphics elements or make them hidden again to wait for next time
Every questdlg or inputdlg call (and even warndlg and errordlg) work the way just described: they create a new modal figure, put up some content in it, uiwait() for the user to click a button or close the figure, and then delete the temporary figure if it still exists. Such things have their uses -- but this sequence is not how permanent UI elements such as radio buttons are handled -- those are handled by the "sit and do nothing until the user triggers a callback" process I describe above.
@Walter. Sorry but your last explanation is too much complicated for me to understand. Can you please explain it with fewer words?
Also, what is wrong with this code I use? I probably have limited knowledge on GUI and how they work. That's why I am here, so I can learn from my mistakes.
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 function Do_plot that follows in 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

Sign in to comment.