0

My program loads data from a file and produces a graph, the user clicks on an area of interest and then analysis is done and a new graph is produced. The program continues asking the user to click on the image until the user presses e to exit the program.

I want the graph that is produced to be a GUI that takes data from my program but I seem to have trouble transferring that data into the GUI function. Here is a quick example of what my program looks like:

load(data)
plot(x,y)
while 1%so that it continues asking for user interaction
     figure(1)
     'click on the point you want or press e to exit'
     [x1,y1,key]=ginput(1)

     f=score(x1,y1)
     %the above is a different function that gives us the data that I want to graph,
     %that are called xf,yf 

     %GUI plot
     figure(1)
     test_gui(xf,yf)

     if (key == 'e')
     display('End')
     break;
     else
     display('next point')
     end
end

My test_gui.m looks like this:

function varargout = test_gui(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
               'gui_Singleton',  gui_Singleton, ...
               'gui_OpeningFcn', @test_gui_OpeningFcn, ...
               'gui_OutputFcn',  @test_gui_OutputFcn, ...
               'gui_LayoutFcn',  [] , ...
               'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before fft_guide is made visible.
function test_gui_OpeningFcn(hObject, eventdata, handles, varargin)


% Choose default command line output for test_gui
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes test_gui wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = test_gui_OutputFcn(hObject, eventdata, handles) 

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
plot (xf,yf)   

The problem is that when I click on the "Push" button, it does not graph anything so there must be something wrong in the way I pass the xf, yf variables. I was wondering if anyone has any ideas about what I am doing wrong, I have not used GUIDE before and it seems I'm lost.

AL B
  • 307
  • 1
  • 14
  • You should implement the loop on the gui start-up function, inside the gui implementation, not outside. There are many ways to store xf and yf, but the recommended by matlab is to do that by using the `handles` variable. I cannot help you now with more details, but you could take a look [here](http://stackoverflow.com/questions/18339335/how-to-create-a-gui-to-play-pause-fast-forward-and-rewind-video-in-matlab/18341693#18341693) to see an example of gui implementation which I explain exactly that. – Werner Sep 11 '13 at 18:40

1 Answers1

0

From the looks of your code, xf and yf are never defined, only f (the result of score). So that could be why you can't see any plots.

Supposing that score dumps xf and yf into the workspace, you must first define them from varargin and then pass them to the callback function using handles, as Werner commented.

% --- Executes just before fft_guide is made visible.
function test_gui_OpeningFcn(hObject, eventdata, handles, varargin)
xf = varargin{0}; yf = varargin{1}; % Get xf and yf from input
handles.xf = xf; handles.yf = yf;  % Put the values in handles
guidata(hObject,handles);   % Save handles so you can use it anywhere in the GUI

And in the callback:

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
plot (handles.xf,handles.yf)

I believe this should work, supposing xf and yf are being correctly define before being passed to the GUI function.

Thales MG
  • 761
  • 5
  • 15
  • Thank you very much. You guessed right I had xf,yf in my workspace (I just did not include it in the code i copied here) but the problem was that I had not included xf = varargin{1}; yf = varargin{2};. Once I included this line it worked great! Thank you again! – AL B Sep 11 '13 at 19:47
  • Glad that it worked for you. If you are satisfied with your results, please accept my answer by clicking the green tick. ;) – Thales MG Sep 11 '13 at 19:57