如何将参数传递给 Matlab GUI 文件
我是 matlab 新手。在使用 Matlab GUI 时,我遇到了如下问题。
.我想要 2 个图形文件,其中一个图形文件调用另一个图形文件。我知道只需从第一个无花果文件中调用第二个无花果文件的名称,我们就可以调用第二个图。但是,我也希望将一些参数从一个Fig文件发送到另一个Fig文件。在这里我需要发送参数并获取这些参数以便进行进一步处理。我一直无法找到解决此问题的方法。如果有人帮助我解决这个问题,我会很高兴。
提前感谢您
i am new to matlab. While working through the Matlab GUI, i faced a problem which is as follows.
.i want to have 2 figure files, with one figure file calling the other. i know that just by calling the name of the 2nd fig file from the first fig file, we can call the 2nd figure. however, i also wish to send some parameters from one fig file to another.here i need to send the arguments and also obtain these parameters so as to do further processing.i havent been able to find a solution to this problem. i would be glad if someone helps me out with this problem.
thanking you in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我发现可以通过三种方法来做到这一点:
方法 1: 使用
setappdata
和getappdata
就像这样:您可以在Fig1的m文件中使用setappdata()来存储您想要传递的任何数据,然后在另一个m文件中调用getappdata()来检索它。这两个函数的参数
0
指定 MATLAB 根工作区,您的程序可以在任何地方访问它(即它是全局的)。因此,当您关闭数字时,数据仍然可用。您可能需要使用rmappdata
删除他们。方法2:使用
guidata
:假设您使用 GUIDE 创建了 GUI,那么您可以访问名为
handles
的结构,该结构在各处传递并且您可以对其进行编辑,因此您可以在 GUI 回调中执行此操作:然后您可以在其他 m 文件中的其他回调中访问
handles.some_var
(因为handles
会自动传递给您):方法 3 :使用
UserData
:从第一个图形中存储您想要的变量:
然后从另一个图形中获取它:(
免责声明:我对 MATLAB 的实际了解并不是那么好,但它有助于找到好的资源,例如 此和这个 ,甚至官方文档中的内容。我在这里写的内容可能是错误的,所以你一定应该查阅文档以获得更多帮助。)
There are three ways I found to do this:
Method 1: Use
setappdata
andgetappdata
like so:You would use setappdata() in the m-file for fig1 to store whatever data you wanted to pass around, and then call getappdata() in another m-file to retrieve it. The argument
0
to the two functions specifies the MATLAB root workspace, which is accessible by your program everywhere (i.e. it is global). As such, when you close your figures that data will still be available. You may want to usermappdata
to remove them.Method 2: Use
guidata
:Assuming you created your GUI with GUIDE, then you have access to a structure called
handles
which is passed around everywhere and which you can edit, and so you can do this in a GUI callback:Then you can access
handles.some_var
elsewhere in some other callback (becausehandles
is automatically passed into it for you) in your other m-file:Method 3: Use
UserData
:Store the variable you want from your first figure:
Then to get it from your other one:
(Disclaimer: My actual knowledge of MATLAB is not all that great, but it helps to be able to find good resources like this and this, and even this from the official docs. What I've written here may be wrong, so you should definitely consult the docs for more help.)
我会这样做(假设您使用的是 GUI 构建器 GUIDE)。
假设您的图形/m 文件名为 firstFigure.fig/m 和 secondFigure.fig/m。在 firstFigure 的代码中,只需调用 secondFigure 并将参数作为实参传递即可:
这些实参将作为变量供 secondFigure 使用回调函数中的 varargin
和
varagin 是一个单元结构;使用 cell2mat 和 char 将其转换回来:
I would do like this (assuming you're using the GUI builder GUIDE).
Let's say that your figures/m-files are named firstFigure.fig/m and secondFigure.fig/m. In the code of firstFigure, just call secondFigure and pass your parameters as arguments:
The arguments will be available to secondFigure as a variable varargin in the callback functions
and
varagin is a cell structure; use cell2mat and char to convert it back:
这可能有帮助:
http://www.mathworks.ch/matlabcentral/newsreader/view_thread/171989
This may help:
http://www.mathworks.ch/matlabcentral/newsreader/view_thread/171989
最简单的方法是将参数包装在元胞数组中并将它们直接发送到 GUI 构造函数。具有两个参数的调用可能如下所示:
然后您可以使用如下代码解压打开函数 (figure2_OpeningFcn) 中的参数:
这些行必须放置在
guidata(hObject, handles); 行之前的某个位置
。然后,您可以在所有其他回调中直接访问handles.par1
和handles.par2
。我假设您正在使用 GUIDE 来生成 GUI。您可以在figure2.m中找到figure2_OpeningFcn,它与figure2.fig位于同一目录中。
注意:您还可以从图窗返回值,
returnvalue = my_figure({my_input})
。如果您也想获得相关说明,请发表评论,我将扩展我的答案。The easiest method is to wrap the parameters in a cell array and send them directly to the GUI constructor. A call with two parameters might look like:
Then you can unpack the arguments in the opening function (figure2_OpeningFcn) with code like:
These lines must be placed somewhere before the line that says
guidata(hObject, handles);
. Then you can accesshandles.par1
andhandles.par2
directly in all the other callbacks.I assume you are using GUIDE to generate your GUI. You can find figure2_OpeningFcn in figure2.m which will be located in the same directory as figure2.fig.
Note: you can also return values from a figure,
returnvalue = my_figure({my_input})
. If you'd like instructions on that too, leave a comment and I'll extend my answer.