4

我如何才能真正获取这些数字并将它们放置在我的 GUI 的轴窗口中?

在下面的示例中,我不确定在我的用户定义代码中放置句柄的位置。我总共有 4 个数字,看起来类似于这个例子。我希望 4 个图形显示在我的 GUI 窗口中而不是单独的窗口中,所以我在 .fig 文件中创建了 4 个轴窗口。

MyVariable这个特定图形的代码根据 in 中的值是 1 还是 0绘制了一个由 66 个黑白矩形组成的网格。如果MyVariable是 1,则为黑色,如果MyVariable为 0,则为白色。我的 .fig GUI 有一个文件,一个文件用于控制 GUI,另一个文件包含链接到 GUI 的用户定义代码。

function test = MyScript(handles)

中间有很多代码

% Initialize and clear plot window 
figure(2); clf;

% Plot the west wall array panels depending on whether or not they are
% shaded or unshaded
for x = 1:11
     for y = 1:6
  if (MyVariable(x,y) == 1)
  rectangle('position', [x-1, y-1, 1, 1] ,'EdgeColor', 'w', 'facecolor', 'k')
  else if(MyVariable(x,y) == 0)
  rectangle('position', [x-1, y-1, 1, 1], 'facecolor', 'w')
end
end
end
end

title('West Wall Array',... 
  'FontWeight','bold')

axis off

上述代码的图形如下所示: 在此处输入图像描述

函数定义包含所有 4 个绘图的所有脚本代码,因为我之前没有将脚本划分为单独的函数。

我的 GUI 脚本代码包含:

   MyScript(handles);
4

2 回答 2

2

您可以通过设置图形的“CurrentAxes”属性来设置轴以在每个绘图命令之前绘制。

在 GUIDE 中,您可以标记给定的轴,例如: http: //www.mathworks.com/help/matlab/creating_guis/gui-with-multiple-axes-guide.html。然后在您的绘图代码中,通过“set”函数和“CurrentAxes”属性指示应绘制到哪个轴。

下面是一个简单的示例,虽然它不使用 GUIDE,但只有基本的子图轴句柄:

% plots in most recent axis by default (ax2)
fig = figure;
ax1 = subplot(1,2,1);
ax2 = subplot(1,2,2);
plot(rand(1,10));

% indicate that you want to plot in ax1 instead
fig = figure;
ax1 = subplot(1,2,1);
ax2 = subplot(1,2,2);
set(gcf, 'CurrentAxes', ax1);
plot(rand(1,10));
于 2013-12-17T22:07:09.847 回答
2

正如 DMR 所说,有必要设置“CurrentAxes”。例如,如果你想用标签名'axis1'绘制到轴上,你应该简单地添加:

axes(handles.axes1);

到你的代码。下面是一个非常简单的示例,其中包含一个“axis1”和“axis2”,使用上面的代码(更正后的)代码。我真的不知道你是想在你的 gui 本身的轴上绘制还是在一个单独的图形上绘制。我希望我涵盖了这两种情况。

function varargout = Test(varargin)

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Test_OpeningFcn, ...
                   'gui_OutputFcn',  @Test_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before Test is made visible.
function Test_OpeningFcn(hObject, eventdata, handles, varargin)


% Choose default command line output for Test
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

plot(handles.axes2,-2*pi:0.1:2*pi,sin(-2*pi:0.1:2*pi));

% Initialize and clear plot window 


MyVariable = ones(11,6);
MyVariable(1:5,1) = 0;

axes(handles.axes1);

for x = 1:11
    for y = 1:6
        if (MyVariable(x,y) == 1)
            rectangle('position', [x-1, y-1, 1, 1] ,'EdgeColor', 'w', 'facecolor', 'k');
        elseif(MyVariable(x,y) == 0)
            rectangle('position', [x-1, y-1, 1, 1], 'facecolor', 'w');
        end
    end
end

title('West Wall Array',... 
  'FontWeight','bold')

figure(2); clf;

for x = 1:11
    for y = 1:6
        if (MyVariable(x,y) == 1)
            rectangle('position', [x-1, y-1, 1, 1] ,'EdgeColor', 'w', 'facecolor', 'k');
        elseif(MyVariable(x,y) == 0)
            rectangle('position', [x-1, y-1, 1, 1], 'facecolor', 'w');
        end
    end
end

title('West Wall Array',... 
  'FontWeight','bold')

function varargout = Test_OutputFcn(hObject, eventdata, handles) 

varargout{1} = handles.output;

您的指南 GUI 应如下所示: 在此处输入图像描述

你的结果是这样的: 在此处输入图像描述

于 2013-12-17T22:29:07.960 回答