📄 editbase.m
字号:
function editbase(strBaseFileName)
%
% Please see editbase.m function body.
%
% Petur Snaeland, 25.04.1995
% Revised Hordur Kvaran, 20.10.97
%##############################################################################
%
% DECLARE GLOBAL HANDLES AND VARIABLES THAT WILL BE USED DURING RULE EDITING
%
% First declare local globals. These variables will be cleared before this
% function is exited.
global hbtnDefault hbtnOK hbtnCancel hbtnChange hlblRules hchkRules ...
hlstRuleNr hlstRules hlstPos hlstVel hlstOut UsedRules...
CurrentRuleNr CurrentRule RuleBase strBaseFile;
% Also needed is one real global variable
if (strBaseFileName=='cartbase.txt'),global CartRules; UsedRules = CartRules;
else global BallRules; UsedRules = BallRules;
end;
ltGray = [0.75 0.75 0.75]; dkGray = [0.5 0.5 0.5];
strBaseFile = strBaseFileName; % Make the passed filename globally accessible.
%##############################################################################
%
% DEFINE POSITION VECTORS FOR THE RULE EDITING WINDOW AND ITS CONTROLS
%
MainPos = get(gcf,'Position'); % The position of the main program window.
MainX = MainPos(1); MainY = MainPos(2); MainW = MainPos(3); MainH = MainPos(4);
gap = 10; BtnW = 100; BtnH = 25; BoxW = 470; BoxH = 230;
posWindow = [MainX+(MainW-BoxW)/2 MainY+(MainH-BoxH)/2 BoxW BoxH];
posRules = [ 10 40 340 180 ];
posRuleNr = [ 10 10 70 20 ];
posIf = [ 95 10 20 30 ];
posPos = [ 111 10 55 20 ];
posAnd = [ 170 10 28 30 ];
posVel = [ 200 10 55 20 ];
posThen = [ 258 10 40 30 ];
posOut = [ 295 10 55 20 ];
posOK = [ 360 BoxH-BtnH-gap BtnW BtnH];
posCancel = [ 360 BoxH-2*BtnH-1.5*gap BtnW BtnH];
posDefault = [ 360 BoxH/2 BtnW BtnH];
posChange = [ 360 10 BtnW BtnH];
%##############################################################################
%
% DEFINE STRINGS FOR THE LISTBOXES AND THE WINDOW TITLE
%
strRuleNr = ' Rule 1 | Rule 2 | Rule 3 | Rule 4 | Rule 5 | Rule 6 | Rule 7 | Rule 8 | Rule 9 ';
strPos = ' ISL | ISM | ISR ';
strVel = ' MVL | SST | MVR ';
strOut = ' PHL | PSL | OOO | PSR | PHR ';
if strcmp(strBaseFile,'cartbase.txt'),
strTitle = 'Edit Rule Base for Cart Controller';
elseif strcmp(strBaseFile,'ballbase.txt'),
strTitle = 'Edit Rule Base for Ball Controller';
end;
%##############################################################################
%
% READ FUZZY RULEBASE FROM THE TEXTFILE
%
[hBaseFile,errormessage] = fopen(strBaseFile,'rt+');
if (hBaseFile==-1),
filepath=which(strBaseFile);
if isempty(filepath),
error(strcat('''strBaseFile''',' not found in search path.'));
else hBaseFile = fopen(filepath,'rt+');
end;
end;
RuleBase = fscanf(hBaseFile,'%s');
fclose(hBaseFile); clear hBaseFile;
%##############################################################################
%
% DEFINE CALLBACK STRINGS FOR THE CONTROLS.
% These strings specify how to respond when the user clicks the respective control.
%
cmdOK = ['global strBaseFile RuleBase UsedRules;',...
'if (strBaseFile==''cartbase.txt''), global CartRules;CartRules = UsedRules;',...
'else global BallRules;BallRules = UsedRules;end;',...
'savebase(''' strBaseFile ''',RuleBase); close(gcf);', ...
'if (strBaseFile==''cartbase.txt''),doevent(''CartTable'');',...
'else doevent(''BallTable'');end;',...
'clear global CurrentRule CurrentRuleNr RuleBase hbtnOK hbtnCancel ', ...
' hchkRules UsedRules RulesOut hbtnChange hbtnDefault hlstRuleNr hlstRules', ...
' hlblRules hlstPos hlstVel hlstOut hBaseFile strBaseFile; '];
cmdCancel = ['clear global CurrentRule CurrentRuleNr RuleBase hbtnOK hbtnCancel ', ...
' hbtnChange hbtnDefault hlstRuleNr hlstRules hlblRules ', ...
' hlstPos hlstVel hlstOut hBaseFile strBaseFile; ', ...
'close(gcf);' ];
cmdChange = ['global hbtnChange hlblRules CurrentRule CurrentRuleNr RuleBase; ', ...
'RuleBase((9*(CurrentRuleNr-1)+1):(9*(CurrentRuleNr-1)+9)) = CurrentRule; ', ...
'set(hlblRules(CurrentRuleNr), ''String'', ', ...
'['' Rule '' num2str(CurrentRuleNr) '': IF '' CurrentRule(1:3) '' AND '' ', ...
'CurrentRule(4:6) '' THEN '' CurrentRule(7:9) ]); ', ...
'set(hbtnChange,''Enable'',''off''); '];
cmdRuleNr = ['global hlblRules hlstRuleNr hbtnChange CurrentRule CurrentRuleNr RuleBase ', ...
' hlstPos hlstVel hlstOut; ', ...
'CurrentRuleNr = get(hlstRuleNr,''Value''); ', ...
'CurrentRule = RuleBase((9*(CurrentRuleNr-1)+1):(9*(CurrentRuleNr-1)+9)); ', ...
'for i=1:9, ', ...
' set(hlblRules(i),''BackgroundColor'',[0.75 0.75 0.75]); ', ...
'end; ', ...
'set(hlblRules(CurrentRuleNr),''BackgroundColor'',''w''); ', ...
'if ~isempty(findstr(''ISL'',CurrentRule)), set(hlstPos,''Value'',1); end; ', ...
'if ~isempty(findstr(''ISM'',CurrentRule)), set(hlstPos,''Value'',2); end; ', ...
'if ~isempty(findstr(''ISR'',CurrentRule)), set(hlstPos,''Value'',3); end; ', ...
'if ~isempty(findstr(''MVL'',CurrentRule)), set(hlstVel,''Value'',1); end; ', ...
'if ~isempty(findstr(''SST'',CurrentRule)), set(hlstVel,''Value'',2); end; ', ...
'if ~isempty(findstr(''MVR'',CurrentRule)), set(hlstVel,''Value'',3); end; ', ...
'if ~isempty(findstr(''PHL'',CurrentRule)), set(hlstOut,''Value'',1); end; ', ...
'if ~isempty(findstr(''PSL'',CurrentRule)), set(hlstOut,''Value'',2); end; ', ...
'if ~isempty(findstr(''OOO'',CurrentRule)), set(hlstOut,''Value'',3); end; ', ...
'if ~isempty(findstr(''PSR'',CurrentRule)), set(hlstOut,''Value'',4); end; ', ...
'if ~isempty(findstr(''PHR'',CurrentRule)), set(hlstOut,''Value'',5); end; ', ...
'set(hbtnChange,''Enable'',''off''); '];
cmdDefault = ['global hbtnChange hlblRules hchkRules UsedRules RuleBase; ', ...
'RuleBase = [''ISLMVLPHLISMMVLPSLISRMVLOOOISLSSTPSLISMSSTOOO' , ...
'ISRSSTPSRISLMVROOOISMMVRPSRISRMVRPHR'' ]; ', ...
'for i=1:9, ', ...
' ActiveRule = RuleBase((9*(i-1)+1):(9*(i-1)+9)); ', ...
' set(hlblRules(i), ''Enable'',''on'');',...
' set(hlblRules(i),''String'', ', ...
' ['' Rule '' num2str(i) '': IF '' ActiveRule(1:3) '' AND '' ', ...
' ActiveRule(4:6) '' THEN '' ActiveRule(7:9) ]); ', ...
' UsedRules(i)=1; set(hchkRules(i),''Value'',1);',...
'end; clear ActiveRule;', ...
'set(hbtnChange,''Enable'',''off''); ' cmdRuleNr ];
cmdPos = ['global hlstPos hbtnChange CurrentRule; ', ...
'SelectedSet = get(hlstPos,''Value''); ', ...
'if SelectedSet==1, ', ...
' CurrentRule(1:3) = ''ISL''; ', ...
'elseif SelectedSet==2, ', ...
' CurrentRule(1:3) = ''ISM''; ', ...
'elseif SelectedSet==3, ', ...
' CurrentRule(1:3) = ''ISR''; ', ...
'end; ', ...
'set(hbtnChange,''Enable'',''on''); clear SelectedSet; '];
cmdVel = ['global hlstVel hbtnChange CurrentRule; ', ...
'SelectedSet = get(hlstVel,''Value''); ', ...
'if SelectedSet==1, ', ...
' CurrentRule(4:6) = ''MVL''; ', ...
'elseif SelectedSet==2, ', ...
' CurrentRule(4:6) = ''SST''; ', ...
'elseif SelectedSet==3, ', ...
' CurrentRule(4:6) = ''MVR''; ', ...
'end; ', ...
'set(hbtnChange,''Enable'',''on''); clear SelectedSet; '];
cmdOut = ['global hlstOut hbtnChange CurrentRule; ', ...
'SelectedSet = get(hlstOut,''Value''); ', ...
'if SelectedSet==1, ', ...
' CurrentRule(7:9) = ''PHL''; ', ...
'elseif SelectedSet==2, ', ...
' CurrentRule(7:9) = ''PSL''; ', ...
'elseif SelectedSet==3, ', ...
' CurrentRule(7:9) = ''OOO''; ', ...
'elseif SelectedSet==4, ', ...
' CurrentRule(7:9) = ''PSR''; ', ...
'elseif SelectedSet==5, ', ...
' CurrentRule(7:9) = ''PHR''; ', ...
'end; ', ...
'set(hbtnChange,''Enable'',''on''); clear SelectedSet; '];
%##############################################################################
%
% LOAD AND GET A HANDLE TO THE INPUT WINDOW, COMMAND BUTTONS AND LISTBOXES.
%
h = figure('Visible','off','NumberTitle','off','MenuBar','none', ...
'Name',strTitle,'Position',posWindow, ...
'Resize','off','Color',ltGray,'KeyPressFcn','disp(''Use the mouse please'');');
hbtnOK = button('OK',posOK,cmdOK);
hbtnCancel = button('Cancel', posCancel,cmdCancel);
hbtnDefault = button('Default Base',posDefault,cmdDefault);
hbtnChange = button('Change Rule',posChange,cmdChange);
hlstRuleNr = dropdown(strRuleNr,posRuleNr,cmdRuleNr);
hlblIf = label(' IF',posIf, ltGray);
hlstPos = dropdown(strPos,posPos,cmdPos);
hlblAnd = label(' AND',posAnd, ltGray);
hlstVel = dropdown(strVel,posVel,cmdVel);
hlblThen = label(' THEN',posThen, ltGray);
hlstOut = dropdown(strOut,posOut,cmdOut);
hfraRules = frame('',posRules,ltGray);
for i=1:9,
cmdchkRules = ['global hchkRules hlblRules UsedRules;',...
'if (get(hchkRules(' num2str(i) '),''Value'')==1),'...
' set(hlblRules(' num2str(i) '),''Enable'',''on'');',...
' UsedRules(' num2str(i) ')=1;',...
'else set(hlblRules(' num2str(i) '),''Enable'',''off'');',...
' UsedRules(' num2str(i) ')=0;',...
'end;'];
hlblRules(i) = uicontrol('Style','text','HorizontalAlignment','left',...
'Position',[ 40 45+18*(9-i) 300 18],'BackgroundColor',ltGray,...
'String',[' Rule ' num2str(i) ': ' readrule(strBaseFile,i) ]);
set(hlblRules(i),'ButtonDownFcn',['global hlstRuleNr; set(hlstRuleNr,''Value'',' num2str(i) ');' cmdRuleNr]);
if (UsedRules(i)==0), set(hlblRules(i),'Enable','off');
else set(hlblRules(i),'Enable','on'); end;
hchkRules(i) = uicontrol('Style','checkbox','Units','pixels','Background',[0.75,0.75,0.75],...
'Position',[20 45+18*(9-i) 18 18],'String','', ...
'Enable','on','Value',UsedRules(i),'Callback', cmdchkRules);
end;
set(hbtnChange,'Enable','off'); % Enable when the user changes a rule.
%##############################################################################
%
% DISPLAY WINDOW AND SELECT THE FIRST RULE
%
set(h,'Visible','on');
eval(cmdRuleNr);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -