简体   繁体   English

如何在MATLAB中将图像从GUI1传递到GUI2

[英]How to pass an Image from GUI1 to GUI2 in MATLAB

This must be easy but for some reasons I can't get this to work. 这一定很容易,但是由于某些原因,我无法使它正常工作。 What I have is 2 GUIs namely GUI1 and GUI2. 我有2个GUI,即GUI1和GUI2。

In GUI1 I read and stored an Image in say A . 在GUI1中,我读取了图像并将其存储在A It also has a PushButton. 它还具有一个PushButton。 Now when I click this Button it should show that image in GUI2's axes1. 现在,当我单击此按钮时,它将在GUI2的axiss1中显示该图像。

I tried setappdata and getappdata but it ends up giving error. 我尝试了setappdatagetappdata但最终给出了错误。 I can't understand the syntax. 我不懂语法。 I'm all new to MATLAB. 我是MATLAB的新手。 Any help is appreciated. 任何帮助表示赞赏。

setappdata / getappdata are discussed in more detail below. setappdata / getappdata将在下面详细讨论。

As mentioned in the comments, you can use setappdata(0, ... / getappdata(0, ... to assign/read data to/from the root object. 如注释中所述,您可以使用setappdata(0, ... / getappdata(0, ...向根对象分配数据/从根对象读取数据。


Excerpted from MATLAB User Interfaces - Passing Data Around User Interface . 摘自MATLAB用户界面-在用户界面周围传递数据 The original authors were Suever and Hoki . 最初的作者是SueverHoki Attribution details can be found on the contributor page . 归属详细信息可以在贡献者页面上找到。 The source is licenced under CC BY-SA 3.0 and may be found in the Documentation archive . 该来源已获得CC BY-SA 3.0的许可,可以在“ 文档”存档中找到。 Reference topic ID: 2883 and example ID: 9775. 参考主题ID:2883和示例ID:9775。

Passing Data Around User Interface 在用户界面周围传递数据

Most advanced user interfaces require the user to be able to pass information between the various functions which make up a user interface. 大多数高级用户界面都要求用户能够在组成用户界面的各种功能之间传递信息。 MATLAB has a number of different methods to do so. MATLAB有许多不同的方法可以做到这一点。


guidata

MATLAB's own GUI Development Environment (GUIDE) prefers to use a struct named handles to pass data between callbacks. MATLAB自己的GUI开发环境(GUIDE)倾向于使用名为handlesstruct在回调之间传递数据。 This struct contains all of the graphics handles to the various UI components as well as user-specified data. struct包含各种UI组件的所有图形句柄以及用户指定的数据。 If you aren't using a GUIDE-created callback which automatically passes handles , you can retrieve the current value using guidata 如果您没有使用GUIDE创建的自动传递handles回调,则可以使用guidata检索当前值。

% hObject is a graphics handle to any UI component in your GUI
handles = guidata(hObject);

If you want to modify a value stored in this data structure, you can modify but then you must store it back within the hObject for the changes to be visible by other callbacks. 如果要修改存储在此数据结构中的值,可以进行修改,但随后必须将其存储回hObject中,以便其他回调可以看到更改。 You can store it by specifying a second input argument to guidata . 您可以通过为guidata指定第二个输入参数来存储它。

% Update the value
handles.myValue = 2;

% Save changes
guidata(hObject, handles)

The value of hObject doesn't matter as long as it is a UI component within the same figure because ultimately the data is stored within the figure containing hObject . 只要是同一figure的UI组件, hObject的值就无关紧要,因为最终数据将存储在包含hObject的图中。

Best for: 最适合:

  • Storing the handles structure, in which you can store all the handles of your GUI components. 存储handles结构,您可以在其中存储GUI组件的所有句柄。
  • Storing "small" other variables which need to be accessed by most callbacks. 存储“小”其他变量,大多数回调都需要访问这些变量。

Not recommended for : 不建议用于

  • Storing large variables which do not have to be accessed by all callbacks and sub-functions (use setappdata / getappdata for these). 存储不必由所有回调和子函数访问的大变量(对此使用setappdata / getappdata )。

setappdata / getappdata setappdata / getappdata

Similar to the guidata approach, you can use setappdata and getappdata to store and retrieve values from within a graphics handle. guidata方法类似,您可以使用setappdatagetappdata从图形手柄中存储和检索值。 The advantage of using these methods is that you can retrieve only the value you want rather than an entire struct containing all stored data. 使用这些方法的优点是,您只能检索所需的值,而不能检索包含所有存储数据的整个struct It is similar to a key/value store. 它类似于键/值存储。

To store data within a graphics object 在图形对象中存储数据

% Create some data you would like to store
myvalue = 2

% Store it using the key 'mykey'
setappdata(hObject, 'mykey', myvalue)

And to retrieve that same value from within a different callback 并从不同的回调中检索相同的值

value = getappdata(hObject, 'mykey');

Note: If no value was stored prior to calling getappdata , it will return an empty array ( [] ). 注意:如果在调用getappdata之前未存储任何值,它将返回一个空数组( [] )。

Similar to guidata , the data is stored in the figure that contains hObject . guidata相似,数据存储在包含hObject的图中。

Best for: 最适合:

  • Storing large variables which do not have to be accessed by all callbacks and sub-functions. 存储不必由所有回调和子函数访问的大变量。

UserData

Every graphics handle has a special property, UserData which can contain any data you wish. 每个图形句柄都有一个特殊的属性UserData ,它可以包含您想要的任何数据。 It could contain a cell array, a struct , or even a scalar. 它可以包含一个单元格数组,一个struct甚至一个标量。 You can take advantage of this property and store any data you wish to be associated with a given graphics handle in this field. 您可以利用此属性,并在此字段中存储希望与给定图形手柄关联的任何数据。 You can save and retrieve the value using the standard get / set methods for graphics objects or dot notation if you're using R2014b or newer. 如果使用R2014b或更高版本,则可以使用用于图形对象的标准get / set方法或点表示法来保存和检索值。

% Create some data to store
mydata = {1, 2, 3};

% Store it within the UserData property
set(hObject, 'UserData', mydata)

% Of if you're using R2014b or newer:
% hObject.UserData = mydata;

Then from within another callback, you can retrieve this data: 然后从另一个回调中,您可以检索以下数据:

their_data = get(hObject, 'UserData');

% Or if you're using R2014b or newer:
% their_data = hObject.UserData;

Best for: 最适合:

  • Storing variables with a limited scope (variables which are likely to be used only by the object in which they are stored, or objects having a direct relationship to it). 存储范围有限的变量(可能仅由存储它们的对象或与其直接相关的对象使用的变量)。

Nested Functions 嵌套函数

In MATLAB, a nested function can read and modify any variable defined in the parent function. 在MATLAB中,嵌套函数可以读取和修改父函数中定义的任何变量。 In this way, if you specify a callback to be a nested function, it can retrieve and modify any data stored in the main function. 这样,如果您将回调指定为嵌套函数,则它可以检索和修改存储在主函数中的任何数据。

function mygui()    
    hButton = uicontrol('String', 'Click Me', 'Callback', @callback);

    % Create a counter to keep track of the number of times the button is clicked
    nClicks = 0;

    % Callback function is nested and can therefore read and modify nClicks
    function callback(source, event)
        % Increment the number of clicks
        nClicks = nClicks + 1;

        % Print the number of clicks so far
        fprintf('Number of clicks: %d\n', nClicks);
    end
end

Best for: 最适合:

  • Small, simple GUIs. 小型,简单的GUI。 (for quick prototyping, to not have to implement the guidata and/or set/getappdata methods). (对于快速原型制作,不必实现guidata和/或set/getappdata方法)。

Not recommended for : 不建议用于

  • Medium, large or complex GUIs. 中型,大型或复杂的GUI。

  • GUI created with GUIDE . 使用GUIDE创建的GUI。


Explicit input arguments 显式输入参数

If you need to send data to a callback function and don't need to modify the data within the callback, you can always consider passing the data to the callback using a carefully crafted callback definition. 如果您需要将数据发送到回调函数,而无需在回调中修改数据,则始终可以考虑使用精心设计的回调定义将数据传递给回调。

You could use an anonymous function which adds inputs 您可以使用添加输入的匿名函数

% Create some data to send to mycallback
data = [1, 2, 3];

% Pass data as a third input to mycallback
set(hObject, 'Callback', @(source, event)mycallback(source, event, data))

Or you could use the cell array syntax to specify a callback, again specifying additional inputs. 或者,您可以使用单元格数组语法指定回调,然后再次指定其他输入。

set(hObject, 'Callback', {@mycallback, data})

Best for: 最适合:

- When the callback needs data to perform some operations but the data variable does not need to be modified and saved in a new state. -当回调需要data来执行某些操作但不需要修改data变量并将其保存为新状态时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM