📄 wordreport.m
字号:
function wr = wordreport(varargin)% wordreport: generate a Microsoft Office Word report% usage: wr = wordreport; % use default options% usage: wr = wordreport(filename); % provide filename for report%% WORDREPORT creates or opens an existing Microsoft Office Word report and% provides helper functions to add some content: text, figures, Simulink% models, Stateflow charts and much more. It also helps in adding or% updating the table of contents, in setting page numbering or in finding% text. Actually, it is possible to mimic almost everything you can do% manually. Just record a macro in Word and analyze the generated VBA code% to find out how to use the ActiveX technology. Inside MATLAB, you can% get a list of available properties with instructions like:% hdlActiveX = wr.getactivexhdl();% get(hdlActiveX);% get(hdlActiveX.Selection);% invoke(hdlActiveX.Selection);%% Returned is a structure containing function handles that enable to% manipulate the Word document in an object-oriented way.%%% Arguments: (input)% filename - (OPTIONAL) - string - Filename of the document to create or% open. The user is responsible for checking file existence before% trying to create or open it. If file does not exist, it is% created.%% DEFAULT: a unique file name generated from current date and time.%% Arguments: (output)% wr - structure of function handles - this structure is used to mimic an% object-oriented programming syntax. For example, the following% syntax can be used to add some text: wr.addtext('Some text')%%% Example:% Create a new document called 'Foo.doc' and add some content (headings,% figures, page breaks, page numbers, table of contents)%% reportFilename = fullfile(pwd,'foo.doc');% wr = wordreport(reportFilename);% % ---% wr.setstyle('Heading 1');% wr.addtext('TOC', [1 1]); % line break before and after text% wr.createtoc(1, 3);% wr.addpagebreak();% % ---% wr.setstyle('Heading 1');% wr.addtext('MATLAB data', [1 1]); % line break before and after text% % ---% wr.setstyle('Heading 2');% wr.addtext('Sample table', [0 1]); % line break after text% dataCell = { ...% 'Test 1', num2str(0.3) , 'OK'; ...% 'Test 2', num2str(1.8) , 'KO'};% [nbRows, nbCols] = size(dataCell);% wr.addtable(nbRows, nbCols, dataCell, [1 1]); % line break before table% % ---% wr.setstyle('Heading 2');% wr.addtext('Sample figure', [0 1]); % line break after text% figure; plot(1:10);% title('Figure 1'); xlabel('Temps [s]'); ylabel('Amplitude [A]');% wr.setstyle('Normal');% wr.addfigure();% % ---% wr.addpagenumbers('wdAlignPageNumberRight');% wr.updatetoc();% % ---% wr.close();% % ---% open(reportFilename);%% More examples:% It is also possible to add a Simulink model view with ADDMODEL, a% Stateflow chart view with ADDSTATEFLOW, a symbol with ADDSYMBOL, ...% Refer to details of helper functions below for further information.%%% Notes:% When invoked without a semicolon (i.e wr = wordreport(...)), a% list of all available helper functions is displayed.% When using SETSTYLE, be careful to use a style name matching your% language settings. For example: 'Heading 1' for english users but% 'Titre 1' for french settings.%%% Acknowledgements:% WriteToWordFromMatlab - MATLAB Central File Exchange% http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=9112&objectType=file%%% Author: Laurent Vaylet% E-mail: laurent.vaylet@gmail.com% Release: 1.0% Release date: 12/10/07% Display some help if neither input nor output is givenif ~nargin && ~nargout help wordreport returnend% Default inputsinArgs = { ... [genvarname(['report' datestr(clock, 'yyyymmddHHMMSS')]) '.doc']}; % filename% Replace default inputs with specified argumentsinArgs(1:nargin) = varargin;% Various initializationsdocFilename = inArgs{1};hdlWordDoc = [];hdlActiveX = [];currentStyle = 'Normal';% Create and open a new document (or open an existing one)CreateDoc();% Assign output argument (structure containing function handles)wr = struct( ... 'addtext', @AddText, ... 'addsymbol', @AddSymbol, ... 'addtable', @AddTable, ... 'addpagenumbers', @AddPageNumbers, ... 'addfigure', @AddFigure, ... 'addmodel', @AddModel, ... 'addstateflow', @AddStateflow, ... 'setstyle', @SetStyle, ... 'goto', @Goto, ... 'createtoc', @CreateTOC, ... 'updatetoc', @UpdateTOC, ... 'addpagebreak', @AddPageBreak, ... 'findtext', @FindText, ... 'select', @Select, ... 'getcolumn', @GetColumn, ... 'getactivexhdl', @GetActiveXHandle, ... 'printmethods', @PrintMethods, ... 'close', @CloseDoc);% --------------------------------------- function CreateDoc % CREATEDOC Create a new Word document using ActiveX and save its handle % Start an ActiveX session with Word hdlActiveX = actxserver('Word.Application'); hdlActiveX.Visible = true; trace(hdlActiveX.Visible); if ~exist(docFilename, 'file'); % Create new document hdlWordDoc = invoke(hdlActiveX.Documents, 'Add'); else % Open existing document hdlWordDoc = invoke(hdlActiveX.Documents, 'Open', docFilename); end end % CreateDoc% --------------------------------------- function AddText(varargin) % ADDTEXT Add some text to the document, using current style % Default inputs inArgs = { ... '', ... % no text [0 1], ... % line break after text [], ... % automatic color }; % Replace default inputs with specified arguments inArgs(1:nargin) = varargin; % Initializations text = inArgs{1}; lineBreaks = inArgs{2}; color = inArgs{3}; % Line breaks before for k = 1:lineBreaks(1) hdlActiveX.Selection.TypeParagraph; end % Apply specified style to text and insert it hdlActiveX.Selection.Style = currentStyle; if ~isempty(color) hdlActiveX.Selection.Font.Color = color; end hdlActiveX.Selection.TypeText(text); hdlActiveX.Selection.Font.Color = 'wdColorAutomatic'; % Set back to default color % Line breaks after for k = 1:lineBreaks(2) hdlActiveX.Selection.TypeParagraph; end end % AddText% --------------------------------------- function AddSymbol(varargin) % ADDSYMBOL Add a symbol represented by an integer % Integer can be found in the Insert/Symbol menu % (176 = degree symbol) % Default inputs inArgs = { ... 176, ... % degree (
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -