【问题标题】:matlab place an image in the center of the guimatlab将图像放在gui的中心
【发布时间】:2012-05-31 22:28:36
【问题描述】:

我正在尝试将图像:'Im' 放置在窗口的中心,如下所示:

我在第一个回调函数中写了:

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

我不确定,但我认为我不需要axesH。另外,axes 让当前坐标区:/

谁能告诉我怎么解决?

这是我的代码:

 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

谢谢大家! :]]

【问题讨论】:

    标签: matlab user-interface axes


    【解决方案1】:

    您目前正在这样做:

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

    image 是一个有点奇怪的函数,就像 MATLAB 中的绘图函数一样 - 它并没有真正遵守您基于其他绘图函数所期望的很多行为。

    来自online documentation for image(下面的重点是我的):

    图像函数有两种形式:

    调用 newplot 来确定绘制位置的高级函数 图形对象并设置以下坐标区属性:

    XLim 和 YLim 包围图片

    从上到下将图像放置在刻度线和网格的前面 线条

    要反转的YDir

    查看到 [0 90]

    将图像添加到当前坐标区的低级函数 调用新情节。低级函数参数列表只能包含 属性名称/属性值对。

    这意味着如果您想将图像添加到预先存在的一组坐标轴中,您必须只使用属性值对。该函数的其他形式都调用newplot

    要将某些内容绘制到您创建的轴上,请使用以下形式:

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

    请注意,设置 y 方向、限制、刻度线等也必须完成,因为高级函数不再负责这些。

    【讨论】:

    • 非常感谢!你的评论给了我帮助,我把图片放在了中间(我更新了我的主题)。
    猜你喜欢
    • 1970-01-01
    • 2011-12-19
    • 1970-01-01
    • 1970-01-01
    • 2017-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多