📄 chaingui.m
字号:
h.MatrixSizesText = uicontrol('style','text', ... 'horiz','right', ... 'units','norm', ... 'backgr', bgc, ... 'string','Matrix size list:', ... 'pos',[.01 .835 .18 .08]);set(h.MatrixSizesText,'units','pix');% Relative offset from left, topset(h.MatrixSizesText, 'units','pix');pos = get(h.MatrixSizesText,'pos');relOffset = [figpos pos];set(h.MatrixSizesText, 'userdata',relOffset);h.MatrixSizes = uicontrol('style','edit', ... 'horiz','left', ... 'units','norm', ... 'backgr', 'w', ... 'callback', @ChangeMatrixSizes, ... 'interruptible','off', ... 'string','{[8 2], [2 4], [4 1], [1 12], [12 3]}', ... 'pos',[.2 .85 .55 .085]);% Relative offset/size from top rightset(h.MatrixSizes, 'units','pix');pos=get(h.MatrixSizes,'pos');relOffset = [figpos pos];set(h.MatrixSizes, 'userdata',relOffset);% Status bar:h.ax_status = axes('pos',[0.0175 0.015 .45 .08]);set(h.ax_status,'vis','off','xlim',[0 1],'ylim',[0 1]);% Main status:h1=line([.02 .98 .98],[.02 .02 .98],'color','w');h2=line([.02 .02 .98],[.02 .98 .98],'color',[1 1 1]*0);h.Status = uicontrol('parent',h.Fig, ... 'style','text', ... 'units','norm', ... 'pos',[.0375 .025 .3 .055], ... 'horiz','left', ... 'backgr',bgc, ... 'string','<status line>');% Prevent status line from moving with resize:set([h.Status h.ax_status],'units','pix');% ---------------------------------------% Toolbar% --------------------------------------icons = load('chaingui_icons.mat');h.SpecifyExpressionButton = uitoggletool( ... 'tooltip','Specify MATLAB Expression', ... 'cdata',icons.mlmode, ... 'clickedcallback',@SpecifyExpression);h.SpecifySizesButton = uitoggletool( ... 'tooltip','Specify Matrix Sizes', ... 'cdata',icons.sizmode, ... 'clickedcallback',@SpecifySizes);h.RandomButton = uipushtool('tooltip','Generate Random Matrices', ... 'separator','on', ... 'cdata',icons.randsiz, ... 'clickedcallback',@ChangeRandomMatrixSizes);h.BuildButton = uipushtool('tooltip','Build M-File Program', ... 'cdata',icons.mfile, ... 'clickedcallback',@GenProgram);h.ReuseOutputButton = uitoggletool('tooltip','Reuse output buffer', ... 'separator','on', ... 'cdata',icons.reuse, ... 'clickedcallback',@ReuseOutputToggle);uipushtool('tooltip','What''s This?', ... 'separator','on', ... 'cdata',icons.whatsthis, ... 'clickedcallback',@HelpWhatsThisCB);% ---------------------------------------% Menus% --------------------------------------% File:%mFile = uimenu('parent',h.Fig,'label','&File');uimenu('parent',mFile,'label','&Print','callback','printdlg(gcbf);');uimenu('parent',mFile,'label','&Close', ... 'callback','close(gcbf);','separator','on');% Options:%mOpts = uimenu('parent',h.Fig,'label','&Options');h.RandomMenu = uimenu('parent',mOpts, ... 'label','Generate &Random Matrices', ... 'accel','R', ... 'callback', @ChangeRandomMatrixSizes);h.BuildMenu = uimenu('parent',mOpts, ... 'label','&Build M-File Program', ... 'accel','B', ... 'callback',@GenProgram);h.SpecifyExpression = uimenu('parent',mOpts, ... 'label','Specify &MATLAB Expression', ... 'callback',@SpecifyExpression, ... 'accel','M', ... 'checked','off','separator','on');h.SpecifySizes = uimenu('parent',mOpts, ... 'label','Specify Matrix &Sizes', ... 'accel','S', ... 'callback', @SpecifySizes, ... 'checked','on');h.ReuseOutput = uimenu('parent',mOpts, ... 'label','Reuse output buffer', ... 'callback', @ReuseOutputToggle, ... 'separator','on', ... 'checked','on');% Window:%uimenu('parent',h.Fig, 'label','&Window', ... 'tag','winmenu', ... 'callback', winmenu('callback'));winmenu(h.Fig); % Initialize the submenu% Help:%mHelp = uimenu('parent',h.Fig,'label','&Help');% Help->Help Topics:uimenu('parent',mHelp, ... 'label','&Help Topics', ... 'callback',@HelpTopicsCB);% Help->What's This? submenu:uimenu('parent',mHelp, ... 'label','&What''s This?', ... 'callback', @HelpWhatsThisCB);% ---------------------------------------% Store GUI data:ud.h = h;set(h.Fig,'userdata', ud);% Protect GUI from user plots, etc:set(h.Fig,'handlevis','callback');% After GUI has all elements in it, install context menu support:install_context_menus(h.Fig);% Setup GUI prompt/callback based on default user spec mode% (checked menu item)ConfigGUIforUserSpec(h.Fig);% ----------------------------------------------function UpdateGUI(hfig)%UpdateGUI Compute problem and update GUI with new resultsud = get(hfig,'userdata');h = ud.h;UpdateStatus(h.Fig,'Computing...');origPtr = get(h.Fig,'pointer');set(h.Fig,'pointer','watch');try, [optArgs,xstr] = GenerateOptimizationArgs(ud); % xxx varList is unused at present set(h.MatrixSizes,'string',xstr); drawnow; % Compute chain multiplication optimization: y = chainmult(optArgs{:}, ReuseOutputOpt(ud), 'opt'); % Update user data: ud.Results = y; set(hfig,'userdata',ud); set(h.MatrixSizes,'foregroundcolor','k'); set(h.ResultList,'string',y.ResultList,'value',1); UpdateStatus(h.Fig,'Ready.'); catch ud.Results = []; set(hfig,'userdata',ud); set(h.MatrixSizes,'foregroundcolor','r'); set(h.ResultList,'string',''); UpdateStatus(h.Fig,'Invalid entry specification.'); % Show error to user: errordlg(lasterr, 'Matrix Entry Error','modal');endset(h.Fig,'pointer',origPtr);ListCallback([],[],h.Fig);% ----------------------------------------------function [optArgs,xstr] = GenerateOptimizationArgs(ud)%GenerateOptimizationArgs% Generate args for optimization engine (optArgs)% and deblanked version of input string (xstr).% The arguments are generated from the user input.%% There are two basic user entry modes to consider:% - Matrix size list% - MATLAB expression% Get input string from edit box:xstr = get(ud.h.MatrixSizes,'string');% Deblank string at both endsxstr = fliplr(deblank(fliplr(deblank(xstr))));if isSizeMode(ud), % Matrix size specification mode: % % Simply evaluate the expression in the base workspace, % as it is a direct argument syntax for the opt engine: sizeArgs = evalin('base',xstr); cplxArgs = zeros(size(sizeArgs)); %varList = num2cell(char('A'+(0:length(optArgs)-1)),1); varList = cellstr(char(double('A') + (0:length(cplxArgs)-1))'); optArgs = {sizeArgs, cplxArgs, varList}; else % MATLAB Expression mode: % optArgs = ParseMATLABExpr(xstr);end% ----------------------------------------------function optArgs = ParseMATLABExpr(str)% ParseMATLABExpr% Returns cell-array list of arguments to chainmult.% Also returns variable name list.% A*B*C -> {'A','B','C'}% U'*V*U -> { 'U''', 'V', 'U'}% sqrt(var1)*ones(5,4)*hamming(8) -> {'sqrt(var1)', 'ones(5,4)', 'hamming(8)'}%% NOTE: Will not work with:% - parentheses surrounding *'s% - math operators other than * unless they occur in parens% Find *'s:imul = find(str=='*');% Create cell-array of variables/expressionsif isempty(imul), c = {str};else c{1}=str(1:imul(1)-1); for i=2:length(imul), c{i}=str(imul(i-1)+1:imul(i)-1); end c{end+1}=str(imul(end)+1:end);end% Eval each expressionfor i=length(c):-1:1, try, d=evalin('base',c{i}); catch d=[]; end sizeArgs{i} = size(d); cplxArgs(i) = ~isreal(d);endvarList = c;optArgs = {sizeArgs, cplxArgs, varList};% ----------------------------------------------function UpdateStatus(hfig,str)% UPDATE_STATUS Update status text.ud = get(hfig,'userdata');set(ud.h.Status,'string',str);% ---------------------------------------------------------------% C O N T E X T M E N U S Y S T E M% --------------------------------------------------------------% ----------------------------------------------function install_context_menus(hfig)install_context_help(hfig); % Help system must be installed firstinstall_result_list_context_menus(hfig);install_matrix_sizes_context_menus(hfig);% ----------------------------------------------function install_matrix_sizes_context_menus(hfig)ud = get(hfig,'userdata');% Get main list context menu:hc = get(ud.h.MatrixSizes,'uicontext');hEntry=[]; % holds handles to each menu itemcbh = @SpecifyExpression;opts={hc,'Specify MATLAB Expression',cbh, 'checked','on'};hEntry(end+1) = createContext(opts);cbh = @SpecifySizes;opts={hc,'Specify Matrix Sizes',cbh};hEntry(end+1) = createContext(opts);% Append handle to main menu item to list of newly created context menus,% and give this list to all peers:hEntry = [hEntry(1) ud.h.SpecifyExpression ... hEntry(2) ud.h.SpecifySizes];set(hEntry,'userdata',hEntry);fixup_context_order(hc);% ----------------------------------------------function install_result_list_context_menus(hfig)ud = get(hfig,'userdata');% Get main list context menu:hc = get(ud.h.ResultList,'uicontext');hEntry=[]; % holds handles to each menu itemcbh = @ReuseOutputToggle;opts={hc,'Reuse output buffer',cbh, 'checked','on'};hEntry(end+1) = createContext(opts);% Append handle to main menu item to list of newly created context menus,% and give this list to all peers:hEntry = [hEntry ud.h.ReuseOutput];set(hEntry,'userdata',hEntry);fixup_context_order(hc);% -----------------------------------------------------------------function hMenu=createContext(opts)% Helper function to append additional context menusargs = {'parent',opts{1}, 'label',opts{2}, 'callback',opts{3:end}};hMenu=uimenu(args{:});% -----------------------------------------------------------------function fixup_context_order(hContext)% Put the first context menu entry (the "What's This?" entry)% last in the context menu list, and turn on the separator% for the "What's This?" entrychildList = get(hContext,'children');childList = childList([end 1:end-1]);set(hContext,'children',childList);set(childList(1),'separator','on');% ---------------------------------------------------------------% H E L P S Y S T E M% --------------------------------------------------------------% --------------------------------------------------------------function HelpWhatsThisBDown(hco, eventStruct)% HelpWhatsThisBDown Button-down function called from the% menu-based "What's This?" functionhfig = gcbf;hOver = hco; % handle to object under pointerDeactivateHelpWhatsThis(hfig);% Dispatch to context help:hc = get(hOver,'uicontextmenu');hm = get(hc,'children'); % menu pointed to by context menutag = get(hm,'tag');% Get the first context menu entry with the 'WT?' prefix:%if ~iscell(tag), tag={tag}; endstrIdx = find(strncmp(tag,'WT?',3));if isempty(strIdx), tag='';elseif length(strIdx)>1, warning('More than one "What''s This?" item in context menu; using the first.'); tag=tag{strIdx(1)};else tag=tag{strIdx};endHelpGeneral([],[],tag);% --------------------------------------------------------------function HelpWhatsThisCB(hco, eventStruct)% HelpWhatsThisCB Toggle the "What's This?" help state% This mimics the context-menu help selection, but allows% cursor-selection of the help topichfig = gcbf;ud = get(hfig,'UserData');if ud.Help.IsActive, DeactivateHelpWhatsThis(hfig);else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -