使用句柄将数字导入MATLAB GUI?(Import figures to MATLAB GUI using handles?)

编程入门 行业动态 更新时间:2024-10-27 06:31:21
使用句柄将数字导入MATLAB GUI?(Import figures to MATLAB GUI using handles?)

我怎样才能将这些数字放在GUI的轴窗口中?

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

根据MyVariable的值是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);

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

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.

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)

lots of code in between

% 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:

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.

My GUI script code contains:

MyScript(handles);

最满意答案

正如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应如下所示: 在此处输入图像描述

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

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);

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;

Your guide GUI should look like this: enter image description here

And your result like this: enter image description here

更多推荐

本文发布于:2023-07-26 02:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1269963.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:句柄   数字   MATLAB   GUI   figures

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!