Alon Shmiel
Alon Shmiel

Reputation: 7121

matlab place an image in the center of the gui

I am trying to place the image: 'Im' in the center of the window, like this:

enter image description here

I wrote in the first callback function:

img = imread(files{k});  %# Read the data from your image file
hAxes = axes('Parent',hOptsGUI,'Units','pixels','Position',[362 242 424 359]);  %#   so the position is easy to define
image(img,'Parent',hAxes);  %# Plot the image
set(hAxes,'Visible','off');          %# Turn the axes visibility off

I am not sure, but I think I dont need axesH. In addition, axes let the current axes :/

Can someone tell me how to solve it?

this is my code:

 function  data = mainGUI(options, files)
      %# current options
      j = 1;
      ops = cellfun(@(c) c(1), options, 'Uniform',false);
      data{j} =  [ops{1:length(ops)}];
      j = j + 1;

      options = cellfun(@(c) c(2:1:end), options, 'Uniform',false);
      clear ops;
      ops = cellfun(@(c) c(1), options, 'Uniform',false);
      opts =  [ops{1:length(ops)}];

     %# create main figure, with plot and options button
     hFig = figure('Name','window 1','Visible','Off');
     callback


     %# options button callback function
     function callback(o,e)
         %# save current options (sharing data between the two GUIs)
         setappdata(hFig, 'opts',opts);

         %# display options dialog and wait for it

         for k=1: length(files)
                 hOptsGUI = secondaryGUI(hFig, options);

                 img = imread(files{k});  %# Read the data from your image file
                 hAxes = axes('Parent',hOptsGUI,'Units','pixels','Position',[362 242 424 359]);  %#   so the position is easy to define
                 image(img,'Parent',hAxes);  %# Plot the image
                 set(hAxes,'Visible','off');          %# Turn the axes visibility off

                 waitfor(hOptsGUI);

                 %# get new options, and update plot accordingly
                 opts = getappdata(hFig, 'opts');
                  data{j} = opts;
                  j = j + 1;
         end
     end
 end

 function hFig = secondaryGUI(hParentFig, options)
     %# create figure

     hFig = figure('Name','Simulation Plot Window','Menubar','none', 'Resize','off', ...
    'WindowStyle','modal', 'Position',[300 300 1150 600]);
     movegui(hFig, 'center');

     options = cellfun(@(c) c(end:-1:1), options, 'Uniform',false);
     num = length(options);

     %# get saved settings
     selected = getappdata(hParentFig, 'opts');

     %# top/bottom panels
     hPanBot = uipanel('Parent',hFig, 'BorderType','none', ...
    'Units','normalized', 'Position',[0 0.0 1 0.2]);
     hPanTop = uipanel('Parent',hFig, 'BorderType','none', ...
    'Units','normalized', 'Position',[0 0.2 1 0.2]);

     %# buttongroups in top panel
     hBtnGrp = zeros(1,num);
     width = 1/num;
     for i=1:num
         %# create button group
         hBtnGrp(i) = uibuttongroup('Parent',hPanTop, ...
        'Units','normalized', 'Position',[(i-1)*width 0 width 1]);
         %# populate it with radio buttons
         height = 1./numel(options{i});
         for j=1:numel(options{i})
             h = uicontrol('Parent',hBtnGrp(i), 'Style','Radio', ...
            'Units','normalized', 'Position',[0.05 (j-1)*height 0.9 height], ...
            'String',options{i}{j});
             %# set initially selected values
             if strcmp(selected{i},options{i}{j})
                 set(hBtnGrp(i), 'SelectedObject',h)
             end
         end
     end

     %# save button in bottom panel
     uicontrol('Parent',hPanBot, 'Style','pushbutton', ...
    'Units','normalized', 'Position',[0.3 0.2 0.4 0.2], ...
    'String','start', 'Callback',@callback)

     %# save button callback function
     function callback(o,e)
         %# get selected values
         hObjs = get(hBtnGrp(:), 'SelectedObject');
         vals = get(cell2mat(hObjs),{'String'});

         %# update settings
         setappdata(hParentFig, 'opts',vals);

         %# close options dialog
         close(hFig)
     end
 end

thanks everyone! :]]

Upvotes: 0

Views: 3441

Answers (1)

tmpearce
tmpearce

Reputation: 12693

You are currently doing this:

% show the images
Im = imread(files{k});
AxesH = axes('Units', 'pixels', 'position', [0.5, 10, 400, 260], 'Visible', 'off');
image(Im, 'Parent', AxesH);

image is a bit of a strange function, as plotting functions in MATLAB go - it doesn't really obey a lot of the behavior you expect based on other plotting functions.

From the online documentation for image (emphasis below is mine):

The image function has two forms:

A high-level function that calls newplot to determine where to draw the graphics objects and sets the following axes properties:

XLim and YLim to enclose the image

Layer to top to place the image in front of the tick marks and grid lines

YDir to reverse

View to [0 90]

A low-level function that adds the image to the current axes without calling newplot. The low-level function argument list can contain only property name/property value pairs.

This means that if you want to add the image to a pre-existing set of axes, you must use only property-value pairs. The other forms of the function all call newplot.

To plot something into the axes you've created, use the following form:

image('Parent', axesH, 'CData', Im); #% add other property-value pairs as needed

Note that things like setting the y-direction, limits, tick marks, etc. will have to be done as well, since the high-level function isn't taking care of that anymore.

Upvotes: 1

Related Questions