%A very simple function plotter to demo GUI building:
%--pushbuttons
%--static/editable text fields
%--drawing axes
%--This version uses an explicit event-loop to check for
%   user actions

clf
clear all

%define the plot button
plotbutton=uicontrol('style','pushbutton',...
   'string','plot', ...
   'fontsize',12, ...
   'position',[100,400,50,20], ...
   'callback', 'drawit=1;');

%define the erase button
erasebutton=uicontrol('style','pushbutton',...
   'string','erase', ...
   'fontsize',12, ...
   'position',[200,400,50,20], ...
   'callback','clrit=1;');

%define the function editable text field
fucfield=uicontrol('style','edit',...
   'string','abs(sin(2*pi*x))', ...
   'fontsize',12, ...
   'position',[220,1,300,20], ...
   'callback','drawit=1;'...
   );

%define the x range editable text field
xrange=uicontrol('style','edit',...
   'string','-1:.01:1', ...
   'fontsize',12, ...
   'position',[100,1,100,20], ...
   'callback','drawit=1;'...
   );

%define some static text
uicontrol('style','text',...
   'string','x-range=',...
   'fontsize',12, ...
   'position',[40,1,60,20]);

%define the erase button
quitbutton=uicontrol('style','pushbutton',...
   'string','Quit', ...
   'fontsize',12, ...
   'position',[300,400,50,20], ...
   'callback','stopit=1;close;');

%Main event loop
stopit=0; %wait for a quit button push
drawit=0; %wait for a draw 
clrit =0; %wait for a clear

while (stopit==0)
   drawnow; %need this in the loop for controls to work
   
   if drawit==1
      drawit=0;
      x=str2num(get(xrange,'string'));
      y=eval(get(fucfield,'string'));
      plot(x,y);
      drawnow;
   end
   
   if clrit==1
      clrit=0;
      cla;
   end
   
end
