0

I have two GUIs. In the first gui I want to plot an input signal (ex: sine signal). 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? Can somebody help me? I really need help.

Alisa
  • 1
  • 2
  • There are actually many many examples of how to do just that; if you Google the title of your question you will see that a good way to go is to use [setappdata](http://www.mathworks.com/help/matlab/ref/setappdata.html) and [getappdata](http://www.mathworks.com/help/matlab/ref/getappdata.html?searchHighlight=getappdata). – Benoit_11 May 07 '15 at 15:05
  • Thank you for your feedback. I already google it and yes I found many solution but unfortunately I don't really quite understand the example. Can you help? – Alisa May 07 '15 at 15:21
  • Yep sure! Are you building the GUIs with GUIDE? – Benoit_11 May 07 '15 at 15:23
  • Thanks, I really need help! Yes, I use GUIDE. I named my first GUI as 'sine_signal' (tag: gui_sine) and my second GUI as 'filtering' (tag: gui_filtering). – Alisa May 07 '15 at 15:35
  • 2
    You probably want to read this: [What's the "right" way to organize GUI code?](http://stackoverflow.com/questions/20064610/whats-the-right-way-to-organize-gui-code) – Robert Seifert May 07 '15 at 18:06
  • And your question is very very similar to this one: [(MATLAB) Can I re-plot imported data from axes in GUI to new figure](http://stackoverflow.com/questions/28704239/matlab-can-i-re-plot-imported-data-from-axes-in-gui-to-new-figure-using-copyfi/), you should have a look at it and at the answer. – Hoki May 08 '15 at 12:52

1 Answers1

2

Here is some code to get you going about using setappdata and getappdata. 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.

I created 2 programmatic GUIs. The code looks a bit different than when GUIs are designed with GUIDE, but the principle is exactly the same. 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. In the 1st GUI (sine_signal) the sine wave is created and displayed. Pressing the pushbutton opens the 2nd GUI (gui_filtering) and calls setappdata. Once you press the pushbutton of that 2nd GUI, setappdata is called to fetch the data and plot it.

Here is the code for both GUIs. You can save them as .m files and press "run" in the sine_signal function.

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

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:

1) You run the 1st GUI (sine signal):

enter image description here

2) After pressing the pushbutton to open the 2nd GUI:

enter image description here

3) After pressing the pushbutton of the 2nd GUI to load/display the data:

enter image description here

That's about it. Hope that helps!

Benoit_11
  • 13,905
  • 2
  • 24
  • 35
  • Heyy thank you very much for your feedback and your help! I will study and try to understand the programming. I will let you know later on! :) – Alisa May 08 '15 at 04:54