使用手柄将图形导入MATLAB GUI吗? [英] Import figures to MATLAB GUI using handles?

查看:127
本文介绍了使用手柄将图形导入MATLAB GUI吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能真正地将这些图形放置在GUI的轴窗口中?

How can I literally take these figures and place them in the axes windows of my GUI?

在下面的示例中,我不确定将句柄放置在用户定义的代码中的哪个位置.我总共有4个数字,看起来与此示例相似.我希望将4个图形显示在我的GUI窗口中,而不是在单独的窗口中显示,因此我在.fig文件中创建了4个坐标轴窗口.

I am not sure where to place handles in my user-defined code in the example below. I have 4 figures in total which look similar to this example. I want the 4 figures to be displayed in my GUI window and not in separate windows, so i've created 4 axes windows in the .fig file.

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

The code for this particular figure draws a grid of 66 black and white rectangles based on whether or not a value in MyVariable is a 1 or a 0. Black if MyVariable is a 1, White if MyVariable is 0. I have a file for my .fig GUI, one file to control the GUI and one with user-defined code that links to the 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

上面的代码如下图所示:

The figure for the above code looks like this:

函数定义包含所有4个图的所有脚本代码,因为我早些时候没有将脚本划分为各个函数.

The function definition contains all of my script code for all 4 plots because I didn't partition my script into individual functions earlier on.

我的GUI脚本代码包含:

My GUI script code contains:

   MyScript(handles);

推荐答案

正如DMR所说,必须设置"CurrentAxes".例如,如果要以标签名称"axis1"绘制到轴中,则只需添加:

As DMR sais, it's necesary to set the 'CurrentAxes'. For example, if you want to plot into the axis with the tag name 'axis1' you should simply add:

axes(handles.axes1);

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

to your code. Below is a very simple example for a figure containing a 'axis1' and 'axis2' using your code (corrected) code from above. Im not really shure wether you want to plot on an axis on your gui itself or a separate figure. I hope I covered both cases.

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应该如下所示:

Your guide GUI should look like this:

您的结果是这样的:

And your result like this:

这篇关于使用手柄将图形导入MATLAB GUI吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆