% A script file to define a gui to edit parameter values used in the stick-slip
% friction example model

%
%	Stan Quinn December 1996
%   Copyright 1995-2012 The MathWorks, Inc.
%

% define parameter list

names = ['M       ';...
         'K       ';...
         'Fstatic ';... 
         'Fsliding';...
         ];
   
   units = ['mass, kg                 ';...
            'spring rate, N/m         ';...
            'static friction force, N ';...
            'kinetic friction force, N';...
         ];
         

% load parameters into the workspace
if exist('loaded', 'var') ~= 1
   load('sf_stickslipdat');   
end

% create a vector, vars, with which to manipulate and assign parameter values
%
% for each parameter, determine its name
for pindex=1:size(names,1)
  nblanks = 0;
  pwidth = size((names(pindex,:)),2);
  for windex=1:pwidth                 
    if (names(pindex,windex) == ' ') 
      nblanks = nblanks + 1; 
    end;  
  end;
  pwidth = pwidth - nblanks;
  namek=names(pindex,1:pwidth);     % eliminate blanks at the end
  if (exist(namek)==1)
    vars(pindex)=eval(namek);  % parameter value into vector vars
  else
    vars(pindex)=0;
  end;
end;

% establish a figure window for the parameter dialog

fig=figure;

% position and label the window

fwidth=460;
flength=size(names,1)*25+80;
set(fig,'Position',[500-fwidth/2 350-flength/2 fwidth flength]); 
set(fig,'Name','system parameters')

% list parameter names

for pindex=1:size(names,1)
   
  text(pindex) = uicontrol(fig,...
  'Style','text',...
  'Position',[10 (flength-10-25*pindex) 100 20],...
  'String',names(pindex,:));

% parameter values, changeable by user

  edit_handlelist(pindex) = uicontrol(fig,...
  'Style','edit',...
  'Position',[120 (flength-10-25*pindex) 100 20],...
  'String',num2str(vars(pindex)),...
  'UserData',[pindex],...
  'CallBack',['me=gco(fig);',...
     'vars(get(me,''UserData''))=str2num(get(me,''String''));']);

% extra information, such as units

   info(pindex) = uicontrol(fig,...
   'Style','text',...
   'Position',[230 (flength-10-25*pindex) 200 20],...
   'String',units(pindex,:),...
   'HorizontalAlignment','left');


end;

OKbutton = uicontrol(fig,...
  'Style','push',...
  'Position',[10 40 100 20],...
  'String','OK',...
  'CallBack',['for pindex=1:size(names,1) eval([names(pindex,:),''=vars(pindex);'']); end;',...
              'clear names namek fwidth flength pindex pwidth windex nblanks vars ESCbutton OKbutton Savebutton edit_handlelist me text;'...
              'delete(fig)']);

ESCbutton = uicontrol(fig,...
  'Style','push',...
  'Position',[120 40 100 20],...
  'String','Cancel',...
  'CallBack',['clear names namek fwidth flength pindex pwidth windex nblanks vars ESCbutton OKbutton Savebutton edit_handlelist me text;',...
              'delete(fig)']);

DEFAULTbutton = uicontrol(fig,...
  'Style','push',...
  'Position',[230 40 100 20],...
  'String','Default',...
  'CallBack',['load(''sf_stickslipdat'');', ...
              'clear names namek fwidth flength pindex pwidth windex nblanks vars ESCbutton OKbutton Savebutton edit_handlelist me text;',...
              'delete(fig); sf_stickslipedit']);

%***** End
