如何将参数传递给 Matlab GUI 文件

发布于 2024-10-22 07:24:19 字数 231 浏览 1 评论 0原文


我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

明明#如月 2024-10-29 07:24:19

我发现可以通过三种方法来做到这一点:

方法 1: 使用 setappdatagetappdata 就像这样:

setappdata(0,'some_var',value)
some_other_var = getappdata(0,'some_var')

您可以在Fig1的m文件中使用setappdata()来存储您想要传递的任何数据,然后在另一个m文件中调用getappdata()来检索它。这两个函数的参数 0 指定 MATLAB 根工作区,您的程序可以在任何地方访问它(即它是全局的)。因此,当您关闭数字时,数据仍然可用。您可能需要使用 rmappdata 删除他们。

方法2:使用guidata

假设您使用 GUIDE 创建了 GUI,那么您可以访问名为 handles 的结构,该结构在各处传递并且您可以对其进行编辑,因此您可以在 GUI 回调中执行此操作:

handles.some_var = some_value
guidata(hObject,handles)

然后您可以在其他 m 文件中的其他回调中访问 handles.some_var (因为 handles 会自动传递给您):

some_other_var = get(handles.some_var)

方法 3 :使用UserData

从第一个图形中存储您想要的变量:

set(name_of_fig, 'UserData', some_var)

然后从另一个图形中获取它:(

some_other_var = get(name_of_fig, 'UserData')  

免责声明:我对 MATLAB 的实际了解并不是那么好,但它有助于找到好的资源,例如 这个 ,甚至官方文档中的内容。我在这里写的内容可能是错误的,所以你一定应该查阅文档以获得更多帮助。)

There are three ways I found to do this:

Method 1: Use setappdata and getappdata like so:

setappdata(0,'some_var',value)
some_other_var = getappdata(0,'some_var')

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 use rmappdata 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:

handles.some_var = some_value
guidata(hObject,handles)

Then you can access handles.some_var elsewhere in some other callback (because handles is automatically passed into it for you) in your other m-file:

some_other_var = get(handles.some_var)

Method 3: Use UserData:

Store the variable you want from your first figure:

set(name_of_fig, 'UserData', some_var)

Then to get it from your other one:

some_other_var = get(name_of_fig, 'UserData')  

(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.)

幸福%小乖 2024-10-29 07:24:19

我会这样做(假设您使用的是 GUI 构建器 GUIDE)。

假设您的图形/m 文件名为 firstFigure.fig/msecondFigure.fig/m。在 firstFigure 的代码中,只需调用 secondFigure 并将参数作为实参传递即可:

someNumber = 1;
someText = 'test';
aMatrix = rand(3);

secondFigure(someNumber, someText, aMatrix);

这些实参将作为变量secondFigure 使用回调函数中的 varargin

function varargout = secondFigure(varargin) 

function secondFigure_OpeningFcn(hObject, eventdata, handles, varargin)

varagin 是一个单元结构;使用 cell2matchar 将其转换回来:

theNumber = cell2mat(varargin(1));
theText = char(varargin(2));
theTextAgain = cell2mat(varargin(2));
theMatrix = cell2mat(varargin(3));

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:

someNumber = 1;
someText = 'test';
aMatrix = rand(3);

secondFigure(someNumber, someText, aMatrix);

The arguments will be available to secondFigure as a variable varargin in the callback functions

function varargout = secondFigure(varargin) 

and

function secondFigure_OpeningFcn(hObject, eventdata, handles, varargin)

varagin is a cell structure; use cell2mat and char to convert it back:

theNumber = cell2mat(varargin(1));
theText = char(varargin(2));
theTextAgain = cell2mat(varargin(2));
theMatrix = cell2mat(varargin(3));
那支青花 2024-10-29 07:24:19

最简单的方法是将参数包装在元胞数组中并将它们直接发送到 GUI 构造函数。具有两个参数的调用可能如下所示:

figure2({param1, param2})

然后您可以使用如下代码解压打开函数 (figure2_OpeningFcn) 中的参数:

handles.par1 = varargin{1}{1};
handles.par2 = varargin{1}{2};

这些行必须放置在 guidata(hObject, handles); 行之前的某个位置。然后,您可以在所有其他回调中直接访问 handles.par1handles.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:

figure2({param1, param2})

Then you can unpack the arguments in the opening function (figure2_OpeningFcn) with code like:

handles.par1 = varargin{1}{1};
handles.par2 = varargin{1}{2};

These lines must be placed somewhere before the line that says guidata(hObject, handles);. Then you can access handles.par1 and handles.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文