📄 cmbrowse.m
字号:
function cmbrowse(command)
% Workspace browser developed for use with CheckMate
%
% Syntax:
% "cmbrowse"
%
% Description:
% "cmbrowse" opens a workspace browser containing the contents of the
% current workspace. This browser can then be used to navigate among
% the CheckMate data structures.
if (nargin < 1)
command = 'open';
end
switch command
case 'open'
open_browser;
case 'resize'
resize_browser(gcbf);
case 'toggle'
toggle_entry(gcbf,gcbo);
case 'refresh'
refresh_browser(gcbf);
otherwise
error(['Unknown browser command ''' command '''.'])
end
return
% -----------------------------------------------------------------------------
function open_browser()
browser_tag = 'CheckMate Object Browser';
H = findobj(0,'type','figure','Tag',browser_tag);
if isempty(H)
fig = create_browser(browser_tag);
refresh_browser(fig);
else
close(H(2:length(H)));
figure(H(1));
end
return
% -----------------------------------------------------------------------------
function refresh_browser(fig)
hierachy_display = findobj(fig,'Tag','Hierachy Display');
content_display = findobj(fig,'Tag','Content Display');
tree = init_browser_tree;
if isempty(tree)
set(hierachy_display,'Value',1, ...
'String','<workspace empty>','Enable','inactive')
set(content_display,'String','','Enable','inactive')
else
set_browser_data(fig,'tree',tree);
set_browser_data(fig,'map',{});
set_browser_data(fig,'previous_select',0);
set_browser_data(fig,'previous_time',-1);
set(hierachy_display,'Value',1,'Enable','inactive')
set(content_display,'Enable','inactive')
refresh_tree_display(fig)
refresh_content_display(fig)
set(hierachy_display,'Enable','on')
set(content_display,'Enable','on')
end
return
% -----------------------------------------------------------------------------
function fig = create_browser(browser_tag)
width = 700; height = 500;
scsize = get(0,'ScreenSize');
fig = figure;
set(fig,'Tag',browser_tag,'MenuBar','none','NumberTitle','off', ...
'Name','CheckMate Object Browser', ...
'Position',[(scsize(3)-width)/2 (scsize(4)-height)/2 width height], ...
'ResizeFcn','cmbrowse(''resize'');')
figpos = get(fig,'Position');
start_color = [0 0.4 0];
end_color = [0.75 0.8 0.75];
nframes = 50;
for k = 1:nframes
frame_color = start_color+(end_color-start_color)*(((k-1)/(nframes-1))^2);
uicontrol('Style','frame','Units','pixels','Tag','Frame Background', ...
'ForegroundColor',frame_color,'BackgroundColor',frame_color)
end
tree = uicontrol('Style','listbox','Units','pixels', ...
'ForegroundColor',[0 0.1 0.0], ...
'FontName','FixedWidth', ...
'Callback','cmbrowse(''toggle'');', ...
'HorizontalAlignment','left','Tag','Hierachy Display');
content = uicontrol('Style','listbox','Units','pixels', ...
'ForegroundColor',[0 0.1 0.0], ...
'FontName','FixedWidth', ...
'HorizontalAlignment','left','Enable','inactive','Max',2, ...
'Tag','Content Display');
text1 = uicontrol('Style','text','Units','pixels', ...
'ForegroundColor',[1 1 1], ...
'BackgroundColor',start_color, ...
'String','Object Hierachy','Tag','Hierachy Heading', ...
'FontSize',8,'FontUnits','pixels', ...
'FontWeight','Bold', ...
'HorizontalAlignment','left');
text2 = uicontrol('Style','text','Units','pixels','String','Content', ...
'ForegroundColor',[1 1 1], ...
'BackgroundColor',start_color, ...
'Tag','Content Heading', ...
'FontWeight','Bold', ...
'FontSize',8,'FontUnits','pixels','HorizontalAlignment','left');
refresh_button = uicontrol('Style','pushbutton','Units','pixels', ...
'String','Refresh', ...
'Tag','Refresh Button', ...
'Callback','cmbrowse(''refresh'');', ...
'FontSize',8,'FontUnits','pixels','HorizontalAlignment','left');
close_button = uicontrol('Style','pushbutton','Units','pixels', ...
'String','Close', ...
'Tag','Close Button', ...
'Callback','close(gcbf)', ...
'FontSize',8,'FontUnits','pixels','HorizontalAlignment','left');
resize_browser(fig);
return
% -----------------------------------------------------------------------------
function resize_browser(fig)
figpos = get(fig,'Position');
heading_height = 15;
button_height = 30;
button_width = 80;
padding = 10;
tree_width = 50*(figpos(3)-3*padding)/100;
content_width = 50*(figpos(3)-3*padding)/100;
listbox_height = figpos(4)-4*padding-heading_height-button_height;
H = findobj(fig,'Tag','Frame Background');
for k = 1:length(H)
set(H(k),'position', ...
[0 (k-1)*figpos(4)/length(H) figpos(3) figpos(4)/length(H)])
end
treepos = [padding 2*padding+button_height tree_width listbox_height];
contentpos = [2*padding+tree_width 2*padding+button_height content_width listbox_height];
tree = findobj(fig,'Tag','Hierachy Display');
set(tree,'position',treepos);
content = findobj(fig,'Tag','Content Display');
set(content,'position',contentpos)
hierachy_heading = findobj(fig,'Tag','Hierachy Heading');
set(hierachy_heading,'position', ...
[padding figpos(4)-heading_height-padding 60 heading_height])
content_heading = findobj(fig,'Tag','Content Heading');
set(content_heading,'position',...
[2*padding+tree_width figpos(4)-heading_height-padding 60 heading_height])
refresh_button = findobj(fig,'Tag','Refresh Button');
set(refresh_button,'position',...
[figpos(3)-2*button_width-2*padding padding button_width button_height])
close_button = findobj(fig,'Tag','Close Button');
set(close_button,'position',...
[figpos(3)-button_width-padding padding button_width button_height])
return
% -----------------------------------------------------------------------------
function node = init_browser_tree()
node = [];
varlist = evalin('base','who');
for k = 1:length(varlist)
node(k).name = varlist{k};
node(k).class = evalin('base',['class(' varlist{k} ')']);
node(k).path = k;
node(k).evalstr = varlist{k};
node(k).children = [];
end
return
% -----------------------------------------------------------------------------
function display_tree(fig,node)
open = ~isempty(node.children);
depth = length(node.path)-1;
indent = [char(' '*ones(1,2*depth))];
listbox = findobj(fig,'Tag','Hierachy Display');
list = get(listbox,'UserData');
new = length(list)+1;
varsize = evalin('base',['size(' node.evalstr ')']);
varsizestr = '';
for k = 1:length(varsize)
if (k > 1)
varsizestr = [varsizestr 'x'];
end
varsizestr = [varsizestr num2str(varsize(k))];
end
list{new} = [indent node.name ' (' node.class ' ' varsizestr ')'];
set(listbox,'UserData',list);
set_browser_data(fig,['map{' num2str(new) '}'],node.path)
for k = 1:length(node.children)
display_tree(fig,node.children(k))
end
return
% -----------------------------------------------------------------------------
function node = get_node(fig,path)
browser_data = get(fig,'UserData');
for k = 1:length(path)
if (k == 1)
str = ['browser_data.tree(' num2str(path(k)) ')'];
else
str = [str '.children(' num2str(path(k)) ')'];
end
end
node = eval(str);
return
% -----------------------------------------------------------------------------
function set_node(fig,path,val)
browser_data = get(fig,'UserData');
for k = 1:length(path)
if (k == 1)
str = ['browser_data.tree(' num2str(path(k)) ')'];
else
str = [str '.children(' num2str(path(k)) ')'];
end
end
str = [str ' = val;'];
eval(str);
set(fig,'UserData',browser_data)
return
% -----------------------------------------------------------------------------
function toggle_entry(fig,listbox)
previous_time = get_browser_data(fig,'previous_time');
current_time = clock;
set_browser_data(fig,'previous_time',current_time);
val = get(listbox,'Value');
previous_select = get_browser_data(fig,'previous_select');
current_select = get_browser_data(fig,['map{' num2str(val) '}']);
% Check if selection is clicked twice within 0.2 seconds to emulate
% the double-click feature.
if (length(current_select) == length(previous_select)) & ...
all(current_select == previous_select) & ...
(previous_time ~= -1) & (etime(current_time,previous_time) < 0.2)
refresh = 1;
path = current_select;
node = get_node(fig,path);
if ~isempty(node.children)
node.children = [];
set_node(fig,path,node);
else
switch node.class
case 'struct'
node_var = evalin('base',node.evalstr);
if (length(size(node_var)) > 2) | (sum(size(node_var) > 1) > 1)
refresh = 0;
else
if length(node_var) > 1
for k = 1:length(node_var)
node.children(k).name = ['(' num2str(k) ')'];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -