简体   繁体   English

如何在Matlab中从一个GUI到另一个GUI检索数据?

[英]How to retrieve data from one gui to another gui in matlab?

I have two GUIs. 我有两个GUI。 In the first gui I want to plot an input signal (ex: sine signal). 在第一个GUI中,我想绘制一个输入信号(例如:正弦信号)。 My problem is, how can I plot back the same signal in the second GUI after I clicked the 'load' pushbutton in the second GUI? 我的问题是,单击第二个GUI中的“加载”按钮后,如何在第二个GUI中绘制相同的信号? Can somebody help me? 有人可以帮我吗? I really need help. 我真的需要帮助

Here is some code to get you going about using setappdata and getappdata . 这是一些代码,可帮助您开始使用setappdatagetappdata This is very basic and there are things which I did not mention (eg using the handles structure or passing variables as input arguments to functions) but using setappdata and getappdata is a safe way to go. 这是非常基本的,有些事情我没有提到(例如,使用handles结构或将变量作为函数的输入参数传递),但是使用setappdatagetappdata是一种安全的方法。

I created 2 programmatic GUIs. 我创建了2个程序化GUI。 The code looks a bit different than when GUIs are designed with GUIDE, but the principle is exactly the same. 该代码看起来与使用GUIDE设计GUI时有所不同,但是原理是完全相同的。 Take the time to examine it and understand what everything does; 花时间检查它,了解所有操作; that's not too complicated. 不太复杂。

Each GUI is composed of one pushbutton and an axes. 每个GUI由一个按钮和一个轴组成。 In the 1st GUI ( sine_signal ) the sine wave is created and displayed. 在第一个GUI( sine_signal )中,创建并显示正弦波。 Pressing the pushbutton opens the 2nd GUI ( gui_filtering ) and calls setappdata . 按下按钮将打开第二个GUI( gui_filtering )并调用setappdata Once you press the pushbutton of that 2nd GUI, setappdata is called to fetch the data and plot it. 按下第二个GUI的按钮后,将调用setappdata来获取数据并绘制数据。

Here is the code for both GUIs. 这是两个GUI的代码。 You can save them as .m files and press "run" in the sine_signal function. 您可以将它们另存为.m文件,然后在sine_signal函数中按“运行”。

1) sine_signal 1) sine_signal

function sine_signal

clear
clc

%// Create figure and uicontrols. Those will be created with GUIDE in your
%// case.
hFig_sine = figure('Position',[500 500 400 400],'Name','sine_signal');

axes('Position',[.1 .1 .8 .8]);

uicontrol('Style','push','Position',[10 370 120 20],'String','Open gui_filtering','Callback',@(s,e) Opengui_filt);

%// Create values and plot them.
xvalues = 1:100;
yvalues = sin(xvalues).*cos(xvalues);

plot(xvalues,yvalues);

%// Put the x and y values together in a single array.
AllValues = [xvalues;yvalues];

%// Use setappdata to associate "AllValues" with the root directory (0).
%// This way the variable is available from anywhere. You could also
%// associate the data with the GUI itself, using "hFig_sine" instead of "0".

setappdata(0,'AllValues',AllValues);

%// Callback of the pushbutton. In this case it is simply used to open the
%// 2nd GUI.

    function Opengui_filt

        gui_filtering

    end
end

And

2) gui_filtering 2) gui_filtering

function gui_filtering

%// Same as 1st GUI.
figure('Position',[1000 1000 400 400],'Name','sine_signal')

axes('Position',[.1 .1 .8 .8])

%// Pushbutton to load data
uicontrol('Style','push','Position',[10 370 100 20],'String','Load/plot data','Callback',@(s,e) LoadData);

%// Callback of the pushbutton
    function LoadData

        %// Use "getappdata" to retrieve the variable "AllValues".

        AllValues = getappdata(0,'AllValues');

        %// Plot the data
        plot(AllValues(1,:),AllValues(2,:))        
    end
end

To show you the expected output, here are 3 screenshots obtained when: 为了显示预期的输出,以下是在以下情况下获得的3个屏幕截图:

1) You run the 1st GUI ( sine signal ): 1)您运行第一个GUI( sine signal ):

在此处输入图片说明

2) After pressing the pushbutton to open the 2nd GUI: 2)按下按钮打开第二个GUI后:

在此处输入图片说明

3) After pressing the pushbutton of the 2nd GUI to load/display the data: 3)按下第二个GUI的按钮加载/显示数据后:

在此处输入图片说明

That's about it. 就是这样 Hope that helps! 希望有帮助!

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

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