📄 guido.m
字号:
function theResult = guido(theStruct, theFigureName, isModal, thePrecision)
% guido -- Get info via Matlab struct.
% guido('demo') demonstrates itself, using the commands
% given below.
% guido(theStruct, 'theFigureName') presents a dialog
% representing the fieldnames and values of theStruct,
% a Matlab "struct" which must be compatible with
% "getinfo" and "setinfo". If theStruct contains
% embedded structs, nested dialogs are produced for
% them as needed. The empty-matrix [] is returned
% if the main "Cancel" button is pressed. Also, use
% "Cancel" to escape a sub-dialog without saving its
% most recent changes. Fields named "help_..." are
% shown in a separate dialog. If theFigureName is
% not given or is empty, the external name of
% theStruct will be used.
% guido(theStruct, 'theFigureName', isModal) uses modal
% dialogs on non-PCWIN machines if "isModal" evaluates
% to logical(1) TRUE. The default is to use non-modal
% dialogs, which allows interactions with other windows,
% except the Matlab command window. In either case, the
% the routine prevents dialogs from being dismissed out
% of sequence.
% guido(theStruct, 'theFigureName', isModal, thePrecision)
% uses thePrecision (default = 4) when displaying numbers
% in an "edit" control.
%
% Commands used in the demonstration:
%
% s.help = help(mfilename);
% s.anEdit = 'some text';
% s.aNumber = pi;
% s.aCheckbox = {'checkbox' 0};
% s.aRadiobutton = {'radiobutton' 0};
% s.aPopupmenu = {{'red', 'blue', 'green'}, 1};
% s.aSubdialog.aPopupmenu = {{10 20 30}, 1};
% guido(s, [mfilename ' demo'])
% Note: the present m-file can be renamed without
% requiring any changes to the Matlab code itself.
% Copyright (C) 1999 Dr. Charles R. Denham, ZYDECO.
% All Rights Reserved.
% Disclosure without explicit written consent from the
% copyright owner does not constitute publication.
% Version of 15-Dec-1999 13:39:54.
% Updated 25-Jan-2000 11:47:42.
persistent CURRENT_STRUCT
persistent CURRENT_FIGURES
LF = char(10);
CR = char(13);
CRLF = [CR LF];
BABY_BLUE = [8 10 10]/10;
if nargout > 0, theResult = []; end
% Launch a demo if this is not a callback.
if nargin < 1 & isempty(gcbo)
help(mfilename)
ans = feval(mfilename, 'demo');
if nargout > 0
theResult = CURRENT_STRUCT;
else
assignin('caller', 'ans', CURRENT_STRUCT)
end
return
end
% Do the demo.
if nargin > 0 & isequal(theStruct, 'demo')
if nargin < 2, theFigureName = [mfilename ' demo']; end
s = [];
s.help = help(mfilename);
s.anEdit = 'some text';
s.aNumber = pi;
s.aCheckbox = {'checkbox' 0};
s.aRadiobutton = {'radiobutton' 0};
s.aPopupmenu = {{'red', 'blue', 'green'}, 1};
s.aSubdialog.aPopupmenu = {{10 20 30}, 1};
% s.aSubdialog.bSubdialog.bPopupmenu = {{100 200 300}, 1};
disp(s)
assign([mfilename '_demo'], s)
result = feval(mfilename, eval([mfilename '_demo']), '', 0);
assign([mfilename '_result'], result)
if ~isempty(eval([mfilename '_result']))
result = feval(mfilename, eval([mfilename '_result']), '', 0);
assign([mfilename '_result'], result)
end
if nargout > 0
assign('theResult', [mfilename '_result'])
end
return
end
if nargin > 0 & ischar(theStruct)
theCommand = lower(theStruct);
end
if 0 & nargin > 0 & ischar(theStruct)
switch theCommand
case '_keypress_'
theKey = get(gcbf, 'CurrentCharacter');
if any(abs(theKey) < 32)
theCommand = '_okay_';
end
end
end
% Dismiss dialogs in sequence.
if nargin > 0 & ischar(theStruct)
switch theCommand
case {'_okay_', '_cancel_'}
CURRENT_FIGURES(~ishandle(CURRENT_FIGURES)) = [];
if ~isempty(CURRENT_FIGURES) & gcbf ~= CURRENT_FIGURES(end)
figure(CURRENT_FIGURES(end))
h = helpdlg('Dismiss dialogs in sequence.', 'Please...');
tic
while ishandle(h) & toc < 3
drawnow
end
if ishandle(h), delete(h), end
return
end
end
end
% Process a command: okay or cancel.
if nargin > 0 & ischar(theStruct)
switch theCommand
case '_okay_'
CURRENT_STRUCT = get(gcbf, 'UserData');
theControls = findobj(gcbf, 'Type', 'uicontrol', 'Tag', mfilename);
theControls = sort(theControls);
for i = 2:2:length(theControls)
theField = get(theControls(i-1), 'String');
theControl = theControls(i);
theStyle = get(theControl, 'Style');
switch theStyle
case 'pushbutton'
okay = 0;
case 'checkbox'
theValue = get(theControl, 'Value');
okay = 1;
case 'radiobutton'
theValue = get(theControl, 'Value');
okay = 1;
case 'edit'
if isequal(theField, 'help')
okay = 0;
else
theValue = eval(get(theControl, 'String'));
okay = 1;
end
case 'popupmenu'
theValue = get(theControl, 'Value');
okay = 1;
otherwise
okay = 0;
warning([' ## Unsupported control style: ' theStyle])
end
if okay
CURRENT_STRUCT = setinfo(CURRENT_STRUCT, theField, theValue);
end
end
if ~isempty(CURRENT_FIGURES) & any(CURRENT_FIGURES == gcbf)
CURRENT_FIGURES(CURRENT_FIGURES == gcbf) = [];
end
delete(gcbf)
case '_cancel_'
CURRENT_STRUCT = [];
if ~isempty(CURRENT_FIGURES) & any(CURRENT_FIGURES == gcbf)
CURRENT_FIGURES(CURRENT_FIGURES == gcbf) = [];
end
delete(gcbf)
otherwise
disp([' ## Unknown command: ' theCommand])
end
return
end
% Build the dialog if it does not already exist.
if nargin > 0 & isstruct(theStruct)
theStruct = setinfo(theStruct);
if nargin < 2 | isempty(theFigureName)
theFigureName = inputname(1);
if isempty(theFigureName)
theFigureName = 'unnamed';
end
end
if isempty(theFigureName), theFigureName = mfilename; end
f = findobj('Type', 'figure', 'Name', theFigureName, 'Tag', mfilename);
if any(f)
figure(f)
return
end
if nargin < 3, isModal = 0; end
if nargin < 4, thePrecision = 4; end
if all(isModal(:)) & ~any(findstr(computer, 'PCWIN'))
theWindowStyle = 'modal';
else
theWindowStyle = 'normal';
end
theFigures = findobj('Type', 'figure', 'Tag', mfilename);
theFigure = figure( ...
'Name', theFigureName, ...
'WindowStyle', theWindowStyle, ...
'Visible', 'off', ...
'KeyPressFcn', [mfilename ' _keypress_'], ...
'CloseRequestFcn', [mfilename ' _cancel_'], ...
'Tag', mfilename);
if any(theFigures)
pos = get(theFigures(1), 'Position');
left = pos(1);
top = pos(2) + pos(4);
for i = 2:length(theFigures)
p = get(theFigures(i), 'Position');
left = max(pos(1), p(1));
top = min(top, p(2) + p(4));
end
thePosition = get(theFigure, 'Position');
thePosition(1) = left + 20;
thePosition(2) = top - thePosition(4) - 20;
set(theFigure, 'Position', thePosition)
end
theFrame = uicontrol( ...
'Style', 'frame', ...
'Units', 'normalized', ...
'Position', [0 0 1 1], ...
'BackgroundColor', BABY_BLUE);
theControls = [];
theStruct = setinfo(theStruct); % Canonical form.
theFields = fieldnames(theStruct);
for i = 1:length(theFields)
theField = theFields{i};
theValue = getfield(theStruct, theField);
switch class(theValue)
case 'cell'
switch class(theValue{1})
case 'cell'
if length(theValue) > 1
theSetting = theValue{2};
else
theSetting = 1;
theValue = {theValue, theSetting};
theStruct = setfield(theStruct, theField, theValue);
end
theStyle = 'popupmenu';
case 'char'
switch theValue{1}
case 'checkbox'
theStyle = 'checkbox';
if length(theValue) > 1
theSetting = theValue{2};
else
theSetting = 0;
end
case 'radiobutton'
theStyle = 'radiobutton';
if length(theValue) > 1
theSetting = theValue{2};
else
theSetting = 0;
end
otherwise
error([' ## Incompatible control style: ' theValue{1}])
end
end
theControls(end+1) = uicontrol( ...
'Style', 'text', ...
'String', theField);
theControls(end+1) = uicontrol( ...
'Style', theStyle, ...
'String', theValue{1}, ...
'Value', theSetting);
case 'char'
f = findstr(theField, 'help_');
if ~any(f) | f(1) ~= 1
theControls(end+1) = uicontrol( ...
'Style', 'text', ...
'String', theField);
theControls(end+1) = uicontrol( ...
'Style', 'edit', ...
'Max', 1000, ...
'String', ['''' theValue '''']);
else
theHintName = [theField ' ' theFigureName];
theCallback = ...
['hint(get(gcbo, ''UserData''), ''' theHintName ''')'];
theControls(end+1) = uicontrol( ...
'Style', 'text', ...
'String', theField);
theControls(end+1) = uicontrol( ...
'Style', 'pushbutton', ...
'Callback', theCallback, ...
'UserData', theValue, ...
'String', 'Help...');
end
case 'double'
theControls(end+1) = uicontrol( ...
'Style', 'text', ...
'String', theField);
theControls(end+1) = uicontrol( ...
'Style', 'edit', ...
'Max', 1000, ...
'String', mat2str(theValue, thePrecision));
case 'struct'
theCallback = ...
[mfilename '(get(gcbo, ''UserData''), ''' theField ''')'];
theControls(end+1) = uicontrol( ...
'Style', 'text', ...
'String', theField);
theControls(end+1) = uicontrol( ...
'Style', 'pushbutton', ...
'String', 'More...', ...
'Callback', theCallback, ...
'UserData', theValue);
otherwise
disp(class(theValue))
error([' ## Incompatible data type. ' class(theValue)])
end
end
set(theControls(1:2:end), ...
'HorizontalAlignment', 'right', ...
'BackgroundColor', BABY_BLUE)
theControls(end+1) = uicontrol( ...
'Style', 'pushbutton', ...
'String', 'Cancel', ...
'BackgroundColor', [10 2 2]/10, ...
'Callback', [mfilename ' _cancel_']);
theControls(end+1) = uicontrol( ...
'Style', 'pushbutton', ...
'String', 'Okay', ...
'BackgroundColor', [2 10 2]/10, ...
'Callback', [mfilename ' _okay_']);
set(theControls, 'Tag', mfilename)
theLayout = [];
for i = 1:length(theControls)/2
theLayout = [(1:2:length(theControls));
(2:2:length(theControls))].';
end
theLayout = theLayout(:, [1 2 2]);
uilayout(theControls, theLayout, [1 1 18 18]/20)
pos = get(0, 'DefaultUIControlPosition');
width = pos(3) * 6;
height = 0.5 * length(theControls) * pos(4) * 20 / 15;
thePosition = get(theFigure, 'Position');
thePosition(2) = thePosition(2) + thePosition(4) - height;
thePosition(3) = width;
thePosition(4) = height;
set(theFigure, 'Position', thePosition, 'Visible', 'on', ...
'UserData', theStruct)
if any(CURRENT_FIGURES)
CURRENT_FIGURES(~ishandle(CURRENT_FIGURES)) = [];
end
CURRENT_FIGURES(end+1) = theFigure;
% Wait here for the new figure to be deleted. By then, it will
% already have placed its contents in the persistent CURRENT_STRUCT
% item.
waitfor(theFigure)
% Now get the new info from CURRENT_STRUCT and update.
theNewStruct = CURRENT_STRUCT;
theFieldName = theFigureName;
if length(theFigures) < 1
theStruct = theNewStruct;
else
theFigure = gcf;
theStruct = get(theFigure, 'UserData');
if ~isfield(theStruct, theFieldName) & ~isequal(theField, 'help')
disp([' ## No such field: ' theField])
elseif ~isempty(theNewStruct)
theStruct = setfield(theStruct, theFieldName, theNewStruct);
set(theFigure, 'UserData', theStruct)
f = findobj(theFigure, 'Type', 'uicontrol', 'Tag', mfilename);
f = sort(f);
for i = 2:2:length(f)
theField = get(f(i-1), 'String');
if isequal(theField, theFieldName)
set(f(i), 'UserData', theNewStruct)
break
end
end
end
end
if nargout > 0
theResult = theStruct;
else
assignin('caller', 'ans', theStruct);
end
end
% ---------- assign ----------%
function assign(theName, theValue)
% assign -- Assign a value to a name.
% assign('theName', theValue) assigns theValue
% to 'theName' in the caller's workspace. It
% avoids the need to construct an explicit
% assignment statement to be eval-ed.
% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
% All Rights Reserved.
% Disclosure without explicit written consent from the
% copyright owner does not constitute publication.
% Version of 28-May-1998 00:43:58.
if nargin < 2, help(mfilename), return, end
% The following scheme permits the assignment
% of items that have complicated subscripts,
% such as "a{1}(2).b{3}.c = pi".
hasAns = (evalin('caller', 'exist(''ans'', ''var'')') == 1);
if hasAns
ans = evalin('caller', 'ans'); % Save.
end
assignin('caller', 'ans', theValue)
evalin('caller', [theName ' = ans;'])
evalin('caller', 'clear(''ans'')')
if hasAns
assignin('caller', 'ans', ans) % Restore.
end
% ---------- getinfo ----------%
function [theResult, isOkay] = getinfo(theInfo, theField)
% getinfo -- Get field value from an "Info" struct.
% getinfo(theInfo, 'theField') returns the current
% value of 'theField' in theInfo, a struct that
% is compatible with the "uigetinfo" function.
% Non-existent fields return the empty-matrix.
% [theResult, isOkay] = ... returns isOkay = 0
% if an error occurred; otherwise, non-zero.
% getinfo(theInfo) returns a struct containing
% the fields and current selections of theInfo.
% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO.
% All Rights Reserved.
% Disclosure without explicit written consent from the
% copyright owner does not constitute publication.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -